C++ Logo

STD-DISCUSSION

Advanced search

Subject: Discussion: std::atomic<T>::synchronize(std::memory_order)
From: Ryan Nicholl (exaeta_at_[hidden])
Date: 2019-09-06 15:11:34


On a few occasions, I found myself doing the following:

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

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
exaeta_at_[hidden]
678-358-7765



STD-DISCUSSION list run by herb.sutter at gmail.com

Older Archives on Google Groups