C++ Logo

std-discussion

Advanced search

Re: Fwd: Synchronization of atomic notify and wait

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Thu, 21 Jul 2022 09:08:52 +0500
On 21/07/2022 03:44, Marcin Jaczewski wrote:
> śr., 20 lip 2022 o 16:59 <language.lawyer_at_[hidden]> napisał(a):
>> On 20/07/2022 15:08, Marcin Jaczewski via Std-Discussion wrote:
>>> Simply by analogy, if we replace this load by increment. Even relaxed operations should be consistent.
>>
>> RMW operations have special properties: https://timsong-cpp.github.io/cppwp/n4861/atomics.order#10
>> You can't just replace load with RMW, draw some conclusions, and replace RMW with load back leaving the conclusions.
>
> ```
> void wait(T, memory_order = memory_order::seq_cst) const noexcept;
> void store(T, memory_order = memory_order::seq_cst) const noexcept;
> ```
>
> and https://timsong-cpp.github.io/cppwp/n4861/atomics.order#5 :
>
> """
> [Note:
> This definition ensures that S is consistent with the modification
> order of any atomic object M.
> It also ensures that a memory_­order::seq_­cst load A of M gets its
> value either from the last modification of M that precedes A in S or
> from some non-memory_­order::seq_­cst modification of M that does not
> happen before any modification of M that precedes A in S.
> — end note]
> """
>
> For your case this behaves the same as RMV, it could miss some writes
> that are not `seq_cst` but it is not our case there.

What does "behaves the same as RMW" mean?

> This means for the default case this is trivial.

How? The paragraph before the Note describes the case when either of 2 conditions hold:
1) A strongly happens before B
2) A is coherence-ordered before B

If by B you mean load after wake-up and by A write before the invocation of notify, then how seq_cst is different from relaxed?
A can become coherence-ordered before B *IF* B reads the value stored by A (https://timsong-cpp.github.io/cppwp/n4861/atomics.order#3.1)
But if it reads the old value, it does not become coherence-ordered before.
On a single atomic object, "relaxed" does not mean "lazy" and "seq_csq" does not magically make the atomic operation "more eager than relaxed".

This means there is no difference between relaxed and seq_cst here. Still no guarantees to load the new value.

> Probably this:
> https://timsong-cpp.github.io/cppwp/n4861/intro.multithread#intro.races-18
> And `notify` happens before `unblock`, this will make "happens before" chain from relaxed `store` to `load`.

If the unblock were synchronized-with the invocation of notify, then sure, the paragraph you're referring to would apply.

Received on 2022-07-21 04:09:00