-
Notifications
You must be signed in to change notification settings - Fork 790
Description
The famous [atomics.order] p11 states:
Implementations should make atomic stores visible to atomic loads within a reasonable amount of time.
I've recently argued about whether this paragraph makes demands towards atomic loads, or just stores. For example, we could have:
std::atomic_bool should_wait;
// ...
while (should_wait.load()) { }
Relaxed Reading
We can interpret the paragraph as:
Implementations should make atomic stores, which will be used by atomic loads visible within a reasonable amount of time.
No demand is made towards x.load()
, so the following optimization is permissible:
bool b = should_wait.load();
while (b) { } // note: undefined behavior for b == false
// because no forward progress is made
Due to this UB, it is also permissible to optimize it to:
[[assume(not should_wait)]];
Strict Reading
We can also interpret it as:
Implementations should make atomic loads use recent atomic stores within a reasonable amount of time.
This would make the above optimization invalid, despite the fact that x.load()
isn't synchronized in any way.
Proposed Disambiguation
The strict reading is how users expect atomics to behave, and is also matching existing practice in implementations. The following wording is less ambiguous:
-Implementations should make atomic stores visible to atomic loads within a reasonable amount of time.
+Implementations should make atomic stores visible to atomic loads,
+and atomic loads should observe atomic stores,
+both within a reasonable amount of time.
Note: the both is essential to avoid further ambiguity here.