m-ou-se / rust-atomics-and-locks

Code examples, data structures, and links from my book, Rust Atomics and Locks.
Other
1.33k stars 120 forks source link

Relaxed ordering - text mentions 0,5,15 while example has add 5, add 10 #57

Closed jakr closed 2 months ago

jakr commented 2 months ago

Type of error

Typo

Location of the error

https://marabos.nl/atomics/memory-ordering.html#relaxed

Description of the error

Mismatch between code: X.fetch_add(5, Relaxed); X.fetch_add(10, Relaxed);

And text: ... only one possible order of modification of X: 0→5→15. It starts at zero, then becomes five, and is finally changed to fifteen. Threads cannot observe any values from X that are inconsistent with this total modification order. This means that "0 0 0 0", "0 0 5 15", and "0 15 15 15" are some of the possible results from the print statement in the other thread, while an output of "0 5 0 15" or "0 0 10 15" is impossible.

Possible fix: Either change 10 to 15 in the code, or change the text: ... only one possible order of modification of X: 0→5→10. It starts at zero, then becomes five, and is finally changed to ten. Threads cannot observe any values from X that are inconsistent with this total modification order. This means that "0 0 0 0", "0 0 5 10", and "0 10 10 10" are some of the possible results from the print statement in the other thread, while an output of "0 5 0 10" or "0 0 10 15" is impossible.

jakr commented 2 months ago

The text and code are OK. Sorry, my bad. I thought about assignments, not additions

m-ou-se commented 2 months ago

No worries. I still appreciate you took the time to report a potential issue. ^^