Closed xmh0511 closed 9 months ago
[atomics.order] p10 maybe a relevant rule
Atomic read-modify-write operations shall always read the last value (in the modification order) written before the write associated with the read-modify-write operation.
However, this wording can have two interpretations
- The check of the violation is done before the side effect of the RMW is in the modification order
- The check of the violation is done after the side effect of the RMW is in the modification order
With the first interpretation, the initial value false
is indeed the last value before the point when the two RMW
operations' write are added to the modification order. Instead, with the second interpretation, the last RMW
operation(in the modification order) reads the initial value that won't be the last value at the point when the side effect of the RMW
is in the modification order.
The second interpretation presumably is the intent of the wording. It may be clearer if we chang it to the following
Atomic read-modify-write operations shall always read the value from a side effect
X
, whereX
immediately precedes the side effect of the read-modify-write operation in the modification order.
The wording needs to be improved to make it readable. In the current wording, "before" has some ambiguities as said in the issue.
Note "atomically" in [atomics.types.operations] p23. The read-compare-write is a single, indivisible operation ("atomically"). That's inconsistent with the idea that a second read-compare-write somehow would partially overlap (on the same atomic object).
"atomically" does not have a formal interpretation in the current draft. Moreover, based on your interpretation of "atomically", the "atomically" seems to only apply to comparison, in which the read operation is unavoidable, not sure whether the "atomically" ultimately applies to that read. The "atomically" even needs to have meaning "non-interrupt" in this case.
However, the load
operation is also marked with "atomically"
Returns: Atomically returns the value pointed to by this.
IMO, there can be more than one load
operation to concurrently read the atomic object, "atomically" seems to not prevent one operation from excluding others.
As @Eisenwave linked the related question, the answers seem to agree [atomics.order] p10 is that formal interpretation of the original issue, however, this wording is quite subtle and needs some decoration.
The read-compare-write is a single
As exposed in Stackover flow / For purposes of ordering, is atomic read-modify-write one operation or two? and the answer in the question linked by @Eisenwave
People have less or more confusion in this aspect, for example, the opinion in the answer
The current wording avoids treating the atomic RMW as a single operation, because it's not when we consider operations on other objects
In certain circumstances, the read and write operations in a CAS_strong are separated.
This issue is now https://cplusplus.github.io/LWG/issue3980 so I think it could be closed here.
Full name of submitter (unless configured in github; will be published with the issue): Jim X
[atomics.types.operations] p23 says:
Consider this example:
This is the essence of implementing the "spinlock". However, in this example,
spin
is initialized with the valuefalse
, So, can the comparison in the two threads both read this initial valuefalse
and compare equally with the expected valuefalse
? IMO, there is no formal wording in the standard that forbids this possible. [intro.races] p14 just says:If they cannot, where does the relevant wording specify that this is impossible?