我希望下面的程序不会挂起。
如果在(1)中以相反的顺序观察到(2)和(3),它可能会由于丢失通知而挂起:
#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;
}
所以问题是这里会发生什么:
标准状态:
对原子对象M
的原子等待操作的调用通过M