Consider the following example:
#include
int main(){
volatile int value = 0;
std::jthread t1([&](){
auto i = value; // #1
});
std::jthread t2([&](){
value = 1; // #2
});
}
[dcl.type.cv] p5 states:
The semantics of an access through a volatile glvalue are implementation-defined.
So, a conforming implementation can specifies that #1 is equivalent to std::atomic_ref(value).load() and #2 is equivalent to std::atomic_ref(value).store(1), both of which are atomic operations defined in [atomics.ref.generic] by the standard.
However, I wonder why such two operations still violate [intro.races] p17, which states:
Two actions are potentially concurrent if
- they are performed by different threads, or
- [...]
The execution of a program contains a data race if it contains two potentially concurrent conflicting actions, at least one of which is not atomic, and neither happens before the other, except for the special case for signal handlers described below. Any such data race results in undefined behavior ([ub:intro.races.data]).
Specifically, under the scenario where the implementation specifies them as std::atomic_ref(value).load()/std::atomic_ref(value).store(1), why are such two operations still non-atomic operations in the point view of the C++ standard?
A related but not complete answer is here, which says
"Implementation-defined semantics" means the implementation gets to define what the operation does. What an implementation doesn't get to decide is how the operation is classified by the standard.
This arises some questions:
- What does the semantics of the access exactly mean?
- How does the standard classify the operations? That is, which operations are atomic operations, and which are not?
- What's the bound of implementation-defined choices for [dcl.type.cv] p5? Can it really not change the standard's classification of operations whose certain properties are implementation-defined?