Skip to content

Commit e214d50

Browse files
Use std::atomic::fetch_add in modern C++.
1 parent 8f0612c commit e214d50

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

core/src/gauge.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ void Gauge::Decrement(const double value) { Change(-1.0 * value); }
1717
void Gauge::Set(const double value) { value_.store(value); }
1818

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

2732
void Gauge::SetToCurrentTime() {

0 commit comments

Comments
 (0)