Memory Operations in Release Semantics - Memory Ordering of C++11

Viewed 57

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) ?

1 Answers
Related