Hard
Relaxed fetch_add operations between a release store and an acquire load
writer() runs once and performs a release store. Three separate threads then each call bumper() exactly once, performing a relaxed fetch_add on the same atomic counter. reader() spins on an acquire load until it observes counter's value has reached 4, the value produced by the last fetch_add to actually execute, not writer()'s original store of 1. Is reader() guaranteed to print 99?
main.cpp
std::atomic<int> counter{0};
int payload = 0;
void writer() {
payload = 99;
counter.store(1, std::memory_order_release); // heads the release sequence
}
void bumper() { // called once each, by 3 threads
counter.fetch_add(1, std::memory_order_relaxed); // relaxed RMW
}
void reader() {
while (counter.load(std::memory_order_acquire) < 4) {}
std::cout << payload << std::endl; // guaranteed 99?
}28 of 51 in Concurrency