Below is the classical example of why neither func_3 or func_4 may print out (assuming each function runs on its own thread). However, I have yet to find an explanation that satisfies my curiosity for details. Assuming the following event order, If func_1 happens first (x.store), then func_3's x.load will synchronize with thread 1 on x (because they are rel-acq pair). Then it may not print out because y.store hasn't happened by the time thread 3 examine y. Then assuming y.store happen in thread 2 next. It causes a synchronization with thread 4 which get past while condition. The next load of x should synchronize with thread 1 and satisfies the print condition right? The only way thread 4 doesn't print is if compiler can reorder the execution y and x load in thread 4 such that y.load happens after x.load. Is that the reason for the possible outcome of neither print shows up? Hence
std::atomic<bool> x,y;
void func_1() {
x.store(true, std::memory_order_release);
}
void func_2() {
y.store(true, std::memory_order_release);
}
void func_3() {
while(!x.load(std::memory_order_acquire));
if(y.load(std::memory_order_acquire)) {
std::cout << "x == true then also y == true \n";
}
}
void func_4() {
while(!y.load(std::memory_order_acquire));
if(x.load(std::memory_order_acquire)) {
std::cout << "y == true then also x == true \n";
}
}