C++ Logo


Advanced search

Discussion: std::atomic<T>::synchronize(std::memory_order)

From: Ryan Nicholl <exaeta_at_[hidden]>
Date: Fri, 06 Sep 2019 20:11:34 +0000
On a few occasions, I found myself doing the following:

if (! foo.load(std::memory_order_relaxed)) return;

Here, the purpose of the second load operation is actually just to get the acquire synchronization. On visual studio load is marked [[nodiscard]]. The alternative to this is std::atomic_thread_fence, but it is not equivalent, and calls (on arm, GCC 8.2) dmb ishld instead of ldar, which is sub optimal. Because of the stronger guarantee of atomic_thread_fence, this is unavoidable.

That got me thinking, would there be a benefit to a synchronize member function? To explicitly annotate that the operation is intended only for atomic synchronization instead of using the value. It would silence the [[nodiscard]] warnings and also make it more clear what the expression is doing.

Perhaps some architectures would benefit too, assuming the CPU has a way to synchronize a release without doing a write? Not sure on that one at the moment.

Ryan Nicholl

Received on 2019-09-06 15:13:47