Skip to content

Commit

Permalink
Use std::atomic::fetch_add in modern C++.
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain-Geissler-1A committed Dec 8, 2022
1 parent 89e79f2 commit 7cdf763
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/gauge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ void Gauge::Decrement(const double value) { Change(-1.0 * value); }
void Gauge::Set(const double value) { value_.store(value); }

void Gauge::Change(const double value) {
// C++ 20 will add std::atomic::fetch_add support for floating point types
#if __cpp_lib_atomic_float >= 201711L
value_.fetch_add(value);
#else
// Pre-C++ 20 fallback: busy loop (which might be more expansive than using fetch_add).
auto current = value_.load();
while (!value_.compare_exchange_weak(current, current + value)) {
// intentionally empty block
}
#endif
}

void Gauge::SetToCurrentTime() {
Expand Down

0 comments on commit 7cdf763

Please sign in to comment.