Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[atomics.order] Clarify whether p11 makes demands towards loads, or just stores #6376

Closed
Eisenwave opened this issue Jul 14, 2023 · 3 comments · Fixed by #6380
Closed

[atomics.order] Clarify whether p11 makes demands towards loads, or just stores #6376

Eisenwave opened this issue Jul 14, 2023 · 3 comments · Fixed by #6380

Comments

@Eisenwave
Copy link
Contributor

Eisenwave commented Jul 14, 2023

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.

@JohelEGP
Copy link
Contributor

Unrelated to the actual issue,
this reads to me like a "recommended practice" paragraph
(e.g., see #5905).

@jensmaurer
Copy link
Member

Agreed with "recommended practice".

Can we make the phrasing a little more symmetric? for example:
"Atomic stores should be visible to atomic loads, and atomic loads should observe atomic stores, within a reasonable amount of time."

I don't like the suggested "both" placement; what's the ambiguity when leaving it out?

Care to prepare a pull request?

@Eisenwave
Copy link
Contributor Author

"Atomic stores should be visible to atomic loads, and atomic loads should observe atomic stores, within a reasonable amount of time."

I felt like the both is needed, but now that I'm reading it, the comma before within should be disambiguating enough, so that this doesn't just refer to loads.

Care to prepare a pull request?

Will do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants