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?

A

Neither. fetch_add is a read-modify-write, and every RMW is sequentially consistent regardless of the ordering argument passed to it.

B

Only B

C

Only A

D

Both A and B