Template:cpp/atomic/sequentially-consistent-cpp20

Formally,

An atomic operation A on some atomic object M is coherence-ordered-before another atomic operation B on M if any of the following is true: @1@ A is a modification, and B reads the value stored by A @2@ A precedes B in the modification order of M @3@ A reads the value stored by an atomic modification X, X precedes B in the modification order, and A and B are not the same atomic read-modify-write operation @4@ A is coherence-ordered-before X, and X is coherence-ordered-before B

There is a single total order S on all operations, including fences, that satisfies the following constraints: @1@ if A and B are operations, and A strongly happens-before B, then A precedes B in S @2@ for every pair of atomic operations A and B on an object M, where A is coherence-ordered-before B:
 * @a@ if A and B are both operations, then A precedes B in S
 * @b@ if A is a operation, and B happens-before a  fence Y, then A precedes Y in S
 * @c@ if a fence X happens-before A, and B is a  operation, then X precedes B in S
 * @d@ if a fence X happens-before A, and B happens-before a  fence Y, then X precedes Y in S

The formal definition ensures that: @1@ the single total order is consistent with the modification order of any atomic object @2@ a load gets its value either from the last  modification, or from some non- modification that does not happen-before preceding  modifications

The single total order might not be consistent with happens-before. This allows more efficient implementation of and  on some CPUs. It can produce surprising results when and  are mixed with.

For example, with and  initially zero,

is allowed to produce, where A happens-before C, but C precedes A in the single total order C-E-F-A of (see Lahav et al).

Note that: @1@ as soon as atomic operations that are not tagged enter the picture, the sequential consistency guarantee for the program is lost @2@ in many cases, atomic operations are reorderable with respect to other atomic operations performed by the same thread