I expect the below program not to hang.
If (2) and (3) are observed in reverse order in (1), it may hang due to lost notification:
#include <atomic>
#include <chrono>
#include <thread>
int main()
{
std::atomic<bool> go{ false };
std::thread thd([&go] {
go.wait(false, std::memory_order_relaxed); // (1)
});
std::this_thread::sleep_for(std::chrono::milliseconds(400));
go.store(true, std::memory_order_relaxed); // (2)
go.notify_all(); // (3)
thd.join();
return 0;
}
So the question is what would happen here:
- The program may hang, I must use fences to prevent it. What exactly fences, where and why?
- The program may not hang. Then how mentioned reordering is prevented? I'm not asking about implementations, I'm asking about standard wording.