Relaxed ordering modern C++

70 views Asked by At

I am going thought the C++ 11 memory model/ordering standard for revision purposes . For std::memory_order::relaxed there is the following example

// Thread 1:
r1 = y.load(std::memory_order_relaxed); // A
x.store(r1, std::memory_order_relaxed); // B
// Thread 2:
r2 = x.load(std::memory_order_relaxed); // C 
y.store(42, std::memory_order_relaxed); // D

And the following statement

is allowed to produce r1 == r2 == 42 because, although A is sequenced-before B within thread 1 and C is sequenced before D within thread 2, nothing prevents D from appearing before A in the modification order of y, and B from appearing before C in the modification order of x. The side-effect of D on y could be visible to the load A in thread 1 while the side effect of B on x could be visible to the load C in thread 2. In particular, this may occur if D is completed before C in thread 2, either due to compiler reordering or at runtime.

In need some further explanation on this. In thread 1 reordering cannot take place since there is a dependency between load in variable r1 and in store in vx. Is this right?

In thread 2 reordering can happen since there is no dependency between and write. Am I correct? Is this problem fixed with the model acquire release? If yes why? Using acquire release model reordering is prevented but if thread 2 for example executed faster than thread 1 y keep value 42, after that in thread 1 x will be 42 as well. What am I missing

0

There are 0 answers