C++ Logo

std-discussion

Advanced search

Re: Fwd: Synchronization of atomic notify and wait

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Thu, 21 Jul 2022 11:14:59 +0200
czw., 21 lip 2022 o 06:09 <language.lawyer_at_[hidden]> napisał(a):
>
> 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?
>

As I interpreted it as "load A of M gets its value either from the
last modification of M that precedes A in S" and it was similar to
RMW.

> > 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.
>

Right, this does not give us a guarantee that `store` precedes `load`
in total order of operation on M.
Adding a comment from Thiago Macieira that we can wait for an update
for a long time then indeed after unblock we still get old value.

Side note, C++ standard allow for Pigeon Post as a way of
synchronizing memory between two machines? :D
Only RMW operations would be problematic as they require an up to date value.
Most of the operations are sync based only when value is visible for a
given thread.

Could this Pigeon be a good model for C++ atomic?

> > 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.

Or at least it should participate with total order of operation on M,
because "happens before" could be too strong for `relaxed`.

Received on 2022-07-21 09:15:11