Let's say we have 2 threads. One producer and one consumer. We have the producer that produce a data, and the consumer that use this data. However the guard is not atomic !
bool isDataReady = false;
int data = 0;
void Producer() {
data = 42;
std::atomic_thread_fence(std::memory_order_release);
isDataReady = true;
}
void Consumer() {
while(!isDataReady);
std::atomic_thread_fence(std::memory_order_acquire);
assert(data == 42);
}
I wonder why is there a data race on isDataReady
.
Normally, the correct code should be to use relaxed
ordering on an atomic bool variable.
Is it because the write (transaction) to isDataReady could be not finished before the read? And even if it is the case, is it really an issue?