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

Incorrect explanation of Sequential Consistency #479

Open
vdrn opened this issue Jan 31, 2025 · 0 comments
Open

Incorrect explanation of Sequential Consistency #479

vdrn opened this issue Jan 31, 2025 · 0 comments

Comments

@vdrn
Copy link

vdrn commented Jan 31, 2025

Quoting from the "Sequential Consistency" part of Atomics:

Intuitively, a sequentially consistent operation cannot be reordered: all accesses on one thread that happen before and after a SeqCst access stay before and after it.

I believe this is wrong, as it states that "all accesses" (which presumably includes data and weaker atomic accesses) cannot be reordered past SeqCst access.

Only other SeqCst accesses cannot be reordered with SeqCst access.
For example, data (and relaxed atomic) accesses can be reordered:

  • before SeqCst stores
  • after SeqCst loads

So, "all accesses" should be replaced with "all SeqCst accesses".

From C++ standard (page 1525, par 7):

In many cases, memory_order::seq_cst atomic operations are reorderable with respect to other atomic operations performed by the same thread.

From Rust Atomics and Locks

The strongest memory ordering is sequentially consistent ordering: Ordering::SeqCst. It includes all the guarantees of acquire ordering (for loads) and release ordering (for stores), and also guarantees a globally consistent order of operations.

This means that every single operation using SeqCst ordering within a program is part of a single total order that all threads agree on. This total order is consistent with the total modification order of each individual variable.

Myth: Sequentially consistent memory ordering can be used for a "release-load" or an "acquire-store."

While SeqCst can stand in for Acquire or Release, it is not a way to somehow create an acquire-store or release-load. Those remain nonexistent. Release only applies to store operations, and acquire only to load operations.

For example, a Release-store does not form any release-acquire relationship with a SeqCst-store. If you need them to be part of a globally consistent order, both operations will have to use SeqCst.

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

No branches or pull requests

1 participant