Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reorder relaxed atomic operations on the same object

http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync

Assuming x is initially 0:

-Thread 1-
x.store (1, memory_order_relaxed)
x.store (2, memory_order_relaxed)

-Thread 2-
y = x.load (memory_order_relaxed)
z = x.load (memory_order_relaxed)
assert (y <= z)

The assert cannot fail.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I don’t understand why the two loads cannot be reordered so that z is read before y which can give z = 1 and y = 2, and so y <= z fails.

>Solution :

Operations on the same object by the same thread be reordered; the modification-order for each object separately is some interleaving of program-order across threads.

Real hardware does that for free, so C++ makes that guarantee available for free. (And doesn’t allow compile-time reordering which could leave the long-term value wrong once the dust settles.)

If the later store was temporarily visible first, the value would have to change twice to have the right final value. Again, real hardware isn’t that weak / crazy, thanks to cache coherency. http://eel.is/c++draft/intro.races#19

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading