Posts

Showing posts with the label memory-order

Confusion about happens before relationship in concurrency

Image
Clash Royale CLAN TAG #URR8PPP Confusion about happens before relationship in concurrency Below is an example given in Concurrency in Action , and the author says the assert may fire, but I don't understand why. assert #include <atomic> #include <thread> #include <assert.h> std::atomic<bool> x,y; std::atomic<int> z; void write_x_then_y() { x.store(true,std::memory_order_relaxed); y.store(true,std::memory_order_relaxed); } void read_y_then_x() { while(!y.load(std::memory_order_relaxed)); if(x.load(std::memory_order_relaxed)) ++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); } As far as I know, in each single thread, sequenced before also means happens before . So in thread a the store to x happens before y , which means x should be modified before y and the result x.store should be visible before y is modified. sequenced bef...