79433244

Date: 2025-02-12 13:35:25
Score: 1
Natty:
Report link

This is from my notes on the topic of atomic modification order and sequentially consistent operation order. Please suggest corrections if you find something off the mark.

In the paper P0668R5: Revising the C++ memory model and in this question, a specific execution of the presented code snippet is discussed. This execution was not legal pre-C++20 but is allowed since C++20. In this execution the way we can derive the modification order of y, as I see it, is as follows.

Quoting the following for reference.

[intro.races/15]

If an operation A that modifies an atomic object M happens before an operation B that modifies M, then A is earlier than B in the modification order of M.

[intro.races/18]

If a side effect X on an atomic object M happens before a value computation B of M, then the evaluation B takes its value from X or from a side effect Y that follows X in the modification order of M.

From the above ordering requirements, the modification order of y in this execution turns out to be (x) -> (1) -> (2) corresponding to the value sequence of 0, 1, 2, 3.

Further, to derive the sequentially consistent operations order, in addition to the above observations, we can observe the following.

Quoting the following for reference.

[atomics.order/4]

There is a single total order S on all memory_order​::​seq_cst operations ... if A and B are memory_order​::​seq_cst operations and A strongly happens before B, then A precedes B in S.

[atomics.order/3.3]

An atomic operation A on some atomic object M is coherence-ordered before another atomic operation B on M if ... A and B are not the same atomic read-modify-write operation, and there exists an atomic modification X of M such that A reads the value stored by X and X precedes B in the modification order of M.

[atomics.order/4.1]

for every pair of atomic operations A and B on an object M, where A is coherence-ordered before B, ... if A and B are both memory_order​::​seq_cst operations, then A precedes B in S.

Combining all above ordering requirements, the sequentially consistent operations order turns out to be (1) -> (2) -> (3) -> (4).

Reasons:
  • RegEx Blacklisted phrase (2.5): Please suggest
  • Long answer (-1):
  • Has code block (-0.5):
Posted by: Dhwani Katagade