我希望以下程序不会挂起。
如果在(1)中反过来观察(2)和(3),可能由于丢失通知而导致挂起:
如果在(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;
}
那么问题就是这里会发生什么:
- 程序可能会挂起,我必须使用"栅栏(fences)"来防止它。"栅栏(fences)"到底是什么,放在哪里以及为什么需要呢?
- 程序可能不会挂起。那么如何防止所提到的重排序(reordering)?我不是在问具体实现,而是在问标准措辞。
WaitOnAddress
,并且希望以跨平台的方式调用它。我有通常基于环形缓冲区的SPSC队列,因此我不担心ABA问题。所以我喜欢这个特性。 - Alex Guteniev