4174. How does [atomics.order] p3 apply when then modification is an initialization?

Section: 32.5.4 [atomics.order] Status: SG1 Submitter: jim x Opened: 2024-11-13 Last modified: 2025-02-07

Priority: 3

View other active issues in [atomics.order].

View all other issues in [atomics.order].

View all issues with SG1 status.

Discussion:

Consider this example

std::atomic<int> v = 0;
// thread 1:
v.load(std::memory_order::seq_cst);
//thread 2:
v.store(1,std::memory_order::seq_cst);

If the load operation reads the value 0, how are load and store operations ordered in the single total order? According to 32.5.4 [atomics.order] p3 (emphasize mine)

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

  1. […]

  2. (3.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 reads the value stored by X and X precedes B in the modification order of M, or

According to 32.5.8.2 [atomics.types.operations] p3 (emphasize mine)

Effects: Initializes the object with the value desired. Initialization is not an atomic operation (6.9.2 [intro.multithread]).

So, how does 32.5.4 [atomics.order] p3 apply to this example such that the load operation precedes the store operation in the single total order S?

[2025-02-07; Reflector poll]

Set priority to 3 after reflector poll. Send to SG1.

LWG found the issue unclear and felt it was missing context that would help understand it properly.

In cplusplus/CWG/issues/641 the following example was given:

std::atomic<bool> a = false;
std::atomic<bool> b = false;
int v = 0;
// thread 1:
a.store(true, seq_cst);
if(b.load(seq_cst)== false){
   v = 1;  // #1
}
//thread 2:
b.store(true, seq_cst);
if(a.load(seq_cst)== false){
   v = 2; // #2
}
To prove whether #1 and #2 can have data race, we should prove whether it's possible that a and b simultaneously read false. This proof equals whether there can be a valid single total order in this case. To determine the order of b.load and b.store when b.load reads the initialization value false, 32.5.4 [atomics.order] p3.3 should apply here. However, the initialization is not an atomic modification such that X cannot be that value.

A possible fix is to amend 32.5.4 [atomics.order]/3.3 to say something like this:

(3.3) A and B are not the same atomic read-modify-write operation, and either
  1. (3.3.1) 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, or
  2. (3.3.2) A reads the initial value of X and B modifies M, or

Proposed resolution: