rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.79k stars 261 forks source link

Data Accesses and synchronized code #301

Closed korawend closed 6 months ago

korawend commented 3 years ago

On the page for Atomics, the Nomicon claims that

It is literally impossible to write correct synchronized code using only data accesses.

but I'm not certain this is true; at least, I was under the impression that with ordinary stores, loads, and memory barriers (but without atomics or the ability to lock the memory bus), it was still possible to synchronize multiple threads using e.g. Dekker's algorithm.

Because of the strength of the statement ("it is literally impossible"), I took this to mean that even the compiler, or a user working with a compiler that did not perform optimizations or re-ordering, would be incapable of synchronizing two threads – which is different than saying "you can't reliably synchronize two threads using Rust and the rustc compiler (without the use of Rust's atomics)".

yvt commented 2 years ago

I think this "data accesses" (allusion to "data race", probably) refers to non-atomic memory accesses as defined by the C++ memory model, which don't have a defined behavior unless properly ordered by atomic memory accesses (which can be read, write, or read-modify-write operations) or other means.