Title
How does [atomics.order] p3 apply when then modification is an initialization?
Status
open
Section
[atomics.order]
Submitter
jim x

Created on 2024-11-13.00:00:00 last changed 3 days ago

Messages

Date: 2025-02-15.00:00:00

[ 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`, [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 [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

Date: 2025-02-07.20:50:58

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 [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 [atomics.types.operations] p3 (emphasize mine)

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

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

History
Date User Action Args
2025-02-07 20:50:58adminsetmessages: + msg14598
2025-02-07 20:50:58adminsetstatus: new -> open
2024-11-13 00:00:00admincreate