#include #include #include bool x=false; std::atomic y; std::atomic z; void write_x_then_y() { x=true; std::atomic_thread_fence(std::memory_order_release); y.store(true,std::memory_order_relaxed); } void read_y_then_x() { while(!y.load(std::memory_order_relaxed)); std::atomic_thread_fence(std::memory_order_acquire); if(x) ++z; } int main() { x=false; y=false; z=0; std::thread a(write_x_then_y); std::thread b(read_y_then_x); a.join(); b.join(); assert(z.load()!=0); }