Fence Pairs With Fence
int data = 0; // plain int, not atomic
std::atomic<bool> ready{false};
// producer thread
data = 42;
std::atomic_thread_fence(std::memory_order_release);
ready.store(true, std::memory_order_relaxed);
// consumer thread
while (!ready.load(std::memory_order_relaxed)) { }
std::atomic_thread_fence(std::memory_order_acquire);
std::cout << data;
Every atomic operation here is relaxed. Is the consumer guaranteed to print 42?
Sign in to answer questions and track your progress
Sign In