Reputation: 2650
I am trying to understand Acquire-Release Semantics
of C++11 Memory Ordering.
What I don't understand is whether the following asserts will ever fail:
#include <atomic>
#include <thread>
#include <cassert>
int global_x{0};
std::atomic_int sync{0};
std::atomic_int atm_y{0};
void thread1()
{
global_x=100;
atm_y.store(200, std::memory_order_relaxed);
sync.store(1,std::memory_order_release);
}
void thread2()
{
while(!sync.load(std::memory_order_acquire)){;}
assert(atm_y.load(std::memory_order_relaxed) == 200);
assert(global_x == 100);
}
int main()
{
global_x=0;
sync=0;
atm_y=0;
std::thread t1(thread1);
std::thread t2(thread2);
t1.join();
t2.join();
}
I know sync.store()
will synchronize-with
sync.load()
because of the acquire-release semantics
but does this semantics guarantee that the memory operations preceding release
will go into the memory(RAM) ?
Upvotes: 3
Views: 64
Reputation: 46
Paraphrasing a very similar example (Listing 5.8) from "C++ Concurrency in Action" by Anthony Williams:
The assert on the load from atm_y will never fail because the store to atm_y happens-before the store to sync (they're in the same thread). Because the store to sync synchronizes-with the load from sync, the store to atm_y also happens-before the load from sync and by extension happens-before the load from atm_y.
The same logic applies to the assignment to global_x. The assignment happens-before the store to sync so the assert will never fire.
Upvotes: 2