Hard
atomic_thread_fence paired with relaxed atomic flags
a and b are plain, non-atomic ints, so they cannot carry a memory_order themselves. flagA and flagB are separate atomics, both stored/loaded with memory_order_relaxed, but producer/consumerA additionally use explicit atomic_thread_fence calls. Is consumerA() guaranteed to print 1?
main.cpp
int a = 0, b = 0; // plain, non-atomic
std::atomic<bool> flagA{false}, flagB{false};
void producer() {
a = 1;
b = 2;
std::atomic_thread_fence(std::memory_order_release);
flagA.store(true, std::memory_order_relaxed);
flagB.store(true, std::memory_order_relaxed);
}
void consumerA() {
while (!flagA.load(std::memory_order_relaxed)) {}
std::atomic_thread_fence(std::memory_order_acquire);
std::cout << a << std::endl; // guaranteed 1?
}22 of 51 in Concurrency