The following is a quote from C++ Standard - Memory Order:
If an atomic store in thread A is tagged memory_order_release and an atomic load in thread B from the same variable is tagged memory_order_acquire, all memory writes (non-atomic and relaxed atomic) that happened-before the atomic store from the point of view of thread A, become visible side-effects in thread B. That is, once the atomic load is completed, thread B is guaranteed to see everything thread A wrote to memory.
The synchronization is established only between the threads releasing and acquiring the same atomic variable. Other threads can see different order of memory accesses than either or both of the synchronized threads.
Consider an atomic variable v
and the following steps:
- Thread
A
stores inv
usingmemory_order_release
- Thread
B
stores inv
usingmemory_order_release
- Thread
C
loads fromv
usingmemory_order_acquire
Is the following statement true:
"thread C
is guaranteed to see everything thread A
or B
wrote to memory."
EDIT: I am moving my comment here to make the point more clear.
The C++ quote that I have up there does not say anything about B
must read what is written by A
. All it says is that A
and B
release/acquire on the same variable. That is exactly, what I am doing in those 3 steps: A
and B
release something, and C
acquires something. Where does it say in the spec that acquire matches with the last release and not necessarily anything before that?