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 9, 2022
1 parent 8f0612c commit e214d50
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/gauge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ 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 e214d50

Please sign in to comment.