C++ Logo

std-discussion

Advanced search

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

From: Daniel Krügler <daniel.kruegler_at_[hidden]>
Date: Sat, 14 Sep 2019 16:30:10 +0200
Am Fr., 6. Sept. 2019 um 22:11 Uhr schrieb Ryan Nicholl via
Std-Discussion <std-discussion_at_[hidden]>:
>
> 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.

I have no strong opinion on the synchronize function suggestion, but
at that point I would suggest to you to file an issue to your compiler
vendor about the applied [[nodiscard]] attribute for this function
given your (IMO reasonable) usecase. The C++ standard does not mark
the atomic load functions with [[nodiscard]].

Thanks,

- Daniel

Received on 2019-09-14 09:32:30