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 Statement about What is Impossible #449

Open
pl170491 opened this issue Apr 28, 2024 · 2 comments
Open

Incorrect Statement about What is Impossible #449

pl170491 opened this issue Apr 28, 2024 · 2 comments

Comments

@pl170491
Copy link

In the chapter about Atomics (Chapter 8.3 sub-section "Data Accesses") there is a very strong statement made about the possibility of synchronizing data between any two or more threads using only normal (i.e. non-atomic) data accesses. I believe there exists an algorithm for sharing data between two threads deterministically and correctly without the use of any atomic operations, and it works even on weakly ordered hardware (or at least so far I've tested it on Apple M3 which is ARM based, as well as the more strongly ordered Intel x86/64). As such, assuming I'm not utterly mistaken, I'd like to request that this statement be altered to better describe reality:

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

The code is located here if you're interested in testing it to prove to yourself that what I say is true on your personal hardware.
https://github.com/alareti/fallout

@numberZero
Copy link

I think you are mistaken.

I believe there exists an algorithm for sharing data between two threads deterministically and correctly without the use of any atomic operations

There is none, by definition of a data race.

and it works even on weakly ordered hardware

Rust is not obliged to translate a ptr::read to a single mov or ldr or what the target CPU provides which makes hardware ordering and such rather irrelevant. In fact, if you have a ptr::read concurrent with a ptr::write to the same location Rust is not obliged to translate either to anything specific as that’s a data race and thus, outright UB.

On the other hand, with atomics the contract is different. Rust is still not obliged to translate accesses to any specific instructions but, it is very much obliged to make each atomic read work as if it was a single atomic read with the ordering as specified (which implicitly allows stronger ordering). Hardware properties don’t affect that either, they basically mean the compiler might be able to emit less barriers on x86 than on ARM to achieve the exact same result (up to no barriers and such for relaxed atomics, typically).

@scottmcm
Copy link
Member

what I say is true on your personal hardware.

What happens today on hardware is largely irrelevant to what Rust's rules are.

If you're relying on the atomicness properties of the CPUs you're running on anyway, why can't you just use the appropriate atomic ordering that does that, and thus get the same instructions but without the data race UB? Especially if you're using std::sync::atomic::compiler_fence(SeqCst) anyway?

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

3 participants