All challenges
Code AnalysisIntermediate 60s

Cross-thread visibility with a relaxed flag

What is the bug here?
Starter code
#include <atomic>
#include <thread>

std::atomic<bool> ready{false};
int data = 0;

void producer() {
    data = 42;
    ready.store(true, std::memory_order_relaxed);
}
void consumer() {
    while (!ready.load(std::memory_order_relaxed)) {}
    std::cout << data;  // may print 0
}

Think you can answer under pressure?

Get matched and answer live questions in 3-minute 1v1 battles. Elo ranking, badge tiers and bragging rights on the line.

Play this in a 3-minute battle