Hello,
I am currently trying to understand the following code example from the book "C++ Concurrency in Action":
// Simplified example in pseudo-C++ (see page 133 for original)
atomic x,y,z;
void write_x() { x.store(true, release); }
void write_y() { y.store(true, release); }
void read_x_then_y() { while (!x.load(acquire)); if (y.load(acquire)) ++z; }
void read_y_then_x() { while (!y.load(acquire)); if (x.load(acquire)) ++z; }
void main() { x=false;y=false;z=0; thread a(write_x); thread b(write_y); thread c(read_x_then_y); thread d(read_y_then_x); join(a,b,c,d); assert(z.load()≠0); }
I don't quite understand how the assertion can fire, as is claimed in the book. How do the lines of code of the four threads have to be arranged in side by side so that z is equal zero. In principle, this can only happen if the lines can be rearranged after the spin-lock. But as I understood it, an Acquire guarantees that Loads and Stores after the Acquire cannot be reordered before the Acquire. And a Load before an Acquire cannot be reordered after an Acquire.
Thanks in advance for clarification.