diff --git a/util/counter_to_rate.h b/util/counter_to_rate.h index c2248edb..51381148 100644 --- a/util/counter_to_rate.h +++ b/util/counter_to_rate.h @@ -6,6 +6,7 @@ #pragma once #include +#include #include @@ -18,7 +19,7 @@ template struct CounterToRate { constexpr CounterToRate(T data, T prev = {}) : value_{std::move(data)}, prev_{std::move(prev)}, count_{1} {} - constexpr T peek_rate() const { return value_ - prev_; } + constexpr T peek_rate() const { std::atomic_thread_fence(std::memory_order_acquire); return value_ - prev_; } constexpr T commit_rate(bool empty_if_unitary = false); constexpr T const &value() const { return value_; } diff --git a/util/counter_to_rate.inl b/util/counter_to_rate.inl index 01031bd2..30df8fe9 100644 --- a/util/counter_to_rate.inl +++ b/util/counter_to_rate.inl @@ -12,7 +12,10 @@ namespace data { template constexpr T CounterToRate::commit_rate(bool empty_if_unitary) { T choice[2] = {value_ - prev_, T{}}; + std::atomic_thread_fence(std::memory_order_release); prev_ = value_; + + std::atomic_thread_fence(std::memory_order_acquire); return choice[empty_if_unitary & (count_ < 2)]; }