In this article, the author mentions that "Happens-Before Does Not Imply Happening Before" and he shows an example to explain.
int A = 0;
int B = 0;
void foo()
{
A = B + 1; // (1)
B = 1; // (2)
}
He says that (2) can actually happen before (1), My question is that what will be the value of A if (2) actually happen before (1), 1 or 2?
foo()
when A and B both equal zero must see both A and B equal to 1 afterfoo()
returns.foo()
is being called, it is possible for the other thread to print A==0 and B==1. In the thread that callsfoo()
, the assignment to A "happens before" the assignment to B, but nothing in your example would transfer that "happens before" relationship to any other thread. The other thread must eventually see A==1 and B==1, but it could see those two assignments happen in either order.