Reorder relaxed atomic operations on the same object

Assuming x is initially 0:

-Thread 1- (1, memory_order_relaxed) (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.

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.

Leave a Reply