Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Synchronizes with" relation

From the cppreference article on std::memory_order:

Synchronizes with: If an atomic store in thread A is a release operation, an atomic load in thread B from the same variable is an acquire operation, and the load in thread B reads a value written by the store in thread A, then the store in thread A synchronizes-with the load in thread B.

Does "…reads a value written by…" have a precise meaning that I’m missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Each atomic variable has (individually) a total order of its modifications, the so-called modification order, which all threads must globally agree on.

Every side effect (i.e. store) on an atomic variable makes up an entry in this modification order.

Each value computation (i.e. load) on a atomic variable takes its value from one of the side effects in this modification order, which aside from some consistency rules, is an unspecified side effect in the modification order.

These consistency rules are given on the cppreference page you linked:

The following four requirements are guaranteed for all atomic operations:

  1. Write-write coherence: If evaluation A that modifies some atomic M
    (a write) happens-before evaluation B that modifies M, then A appears
    earlier than B in the modification order of M.

  2. Read-read coherence: if a value computation A of some atomic M (a
    read) happens-before a value computation B on M, and if the value of A
    comes from a write X on M, then the value of B is either the value
    stored by X, or the value stored by a side effect Y on M that appears
    later than X in the modification order of M.

  3. Read-write coherence: if a value computation A of some atomic M (a
    read) happens-before an operation B on M (a write), then the value of
    A comes from a side-effect (a write) X that appears earlier than B in
    the modification order of M.

  4. Write-read coherence: if a side effect (a write) X on an atomic
    object M happens-before a value computation (a read) B of M, then the
    evaluation B shall take its value from X or from a side effect Y that
    follows X in the modification order of M.

Note that the above uses informal language like "the value of A comes from", which formally should be "the value computation on A takes its value from". The formal specification can be found in [intro.races]/14 and following paragraphs.

"and the load in thread B reads a value written by the store in thread A" is also an informal way to say "and the value computation in thread B takes its value from the side effect in thread A".

Your quote is saying that the synchronizes-with relation occurs only with the particular evaluation that caused the side effect to the atomic variable from which the value computation takes its value.

What this means in particular is that if there are two side effects in the modification order that both store the same value to the atomic, then, taking into account the consistency rules above, it may be possible that a value computation on the atomic will take its value from either one of the two. While you can’t distinguish the two cases by looking at the value produced by the load, it will still affect the synchronizes-with relation and what memory synchronization guarantees you will get.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading