2
比方說,我有1個計數器從值2開始,一些非原子布爾變量和4個線程。C++獲取/釋放排序
//Initialization (happens before any thread execute).
std::atomic<int> count = ATOMIC_VAR_INIT(2);
bool someBoolean = false;
線程1:
count.fetch_sub(1, std::memory_order_release);
線程2:
someBoolean = true;
count.fetch_sub(1, std::memory_order_release);
線程3:
while(count.load(std::memory_order_acquire) != 0);
//Now, we're out of the while loop...
assert(someBoolean);
線程4:
while(std::atomic_thread_fence(std::memory_order_acquire),
count.load(std::memory_order_relaxed) != 0);
//Now, we're out of the while loop...
assert(someBoolean);
線程3或線程4的斷言可能會觸發嗎?