Atomic Is Not Ordered
One producer thread and one consumer thread run this. data is a plain int.
std::atomic<int> counter{0};
std::atomic<bool> ready{false};
int data = 0;
void producer() {
data = 7;
ready.store(true, std::memory_order_release);
counter.fetch_add(1, std::memory_order_relaxed);
}
void consumer() {
while (!ready.load(std::memory_order_acquire)) { }
assert(data == 7); // A
assert(counter.load(std::memory_order_relaxed) == 1); // B
}
Which assertion can fire?
Sign in to answer questions and track your progress
Sign In