Pontakorn Prasertsuk
Pontakorn Prasertsuk

Reputation: 21

Why is the C++ standard definition of 'coherenced-order before' different from the paper it is based on?

This proposal introduced a change to the behavior of sequentially consistent memory model in C++20. The proposal is based on this paper.

The standard introduces a relation called 'coherenced-ordered before' defined as follows:

[atomics]

An atomic operation A on some atomic object M is coherence-ordered before another atomic operation B on M if

  1. A is a modification, and B observes A, or
  2. A precedes B in the modification order, or
  3. A and B are not the same atomic read-modify-write operation, and there exists an atomic modification X of M such that A observes the value stored by X and X precedes B in the modification order of M, or
  4. there exists X such that A is coherence-ordered before X and X is coherence-ordered before B.

This definition seems to be based on the 'extended coherence order' relation defined in the paper above with only one difference: the third point in the standard restricts A and B to only read-modify-write operation (bolded part). However, in the paper, it is possible for A to be an atomic read operation and B to be an atomic write operation as they are defined using a 'read-before' relation.

This seems weird to me since the proposal itself also states that:

The definition of "coherence-ordered before" is essentially standard terminology, but was not previously part of our standard. The third bullet corresponds to what's often called "reads before": A reads a write earlier than B.

Another paper, which introduces the RC20 memory model, also defines 'read-before' ('from-read') without the read-modify-write restriction.

So why is there a mismatch between the standard and the paper it is based on? Are there any reasons why the standard 'weakens' the 'coherence-ordered before' relation?

Considering the following example:

// Thread 1
x.load(std::memory_order::seq_cst); // 0

// Thread 2
x.store(1, std::memory_order::seq_cst);

Assuming that x is initialized with 0, does this mean it is possible for the load to come after the store in the strict total order S? since they do not form a 'strongly-happen-before' relation and neither of them are read-modify-write operations.

Upvotes: 2

Views: 120

Answers (0)

Related Questions