I understand the basic rules for memory ordering in C++11, especially release-acquire ordering. I have a big chunk of memory shared between two threads, where I do not need atomicity, but want to ensure that eventually all the changes done by a thread is visible in another, especially on platforms with relaxed memory model.
Is it ok to simply use an atomic guard var only to trigger memory synchronization? E.g.,
std::atomic<bool> guardVar;
char *shared_mem=get_shared_mem();
(thread 1)
while(true) {
happens_many_things();
do_whatever_I_want_with_shared_mem();
guardVar.store(0, std::memory_order_release);
}
(in thread 2)
while(true) {
guardVar.load(std::memory_order_acquire);
read_shared_mem_no_problem_if_inconsistent();
}
Again, it is not a problem if thread 2 reads a "half-ready" state in the middle of do_whatever_I_want_with_shared_mem(), I just want to ensure that I get all the changes written by thread 1 after a well defined point.
Based on this article it should work, but I do not see solutions like this on the net, and is not easy to test if it really does what I intend.
Is it ok? If it is, is there a more elegant way?