I have read webpage :
http://bartoszmilewski.com/2008/12/01/c-atomics-and-memory-ordering/
and then coding the test source compiled at g++ 4.8.1 , cpu is Intel ...
global var : r1=0;r2=0;x=0;y=0;
Thread1 :
x = 1 ; //line 1
r1 = y ; //line 2
Thread2 :
y = 1 ; //line 3
r2 = x ; //line 4
And I will get r1==0 && r2 == 0 sometimes while run thread1 and thread2 concurrently , I know it is the load of y (line 2) and load of x (line 4) executed before store of x(line 1) , store of y(line 3) ....even strong memory model like intel cpu , load disordered before store still happen , that is why r1==0 && r2 ==0 still happen in this test !!!!
Refering to c++11 memory model , I change source like following :
global vars :
int r1=0,r2=0 ;
atomic<int> x{0} ;
atomic<int> y{0} ;
Thread1 :
x.store(1,memory_order_acq_rel) ;
r1=y.load(memory_order_relaxed) ;
Thread2 :
y.store(1,memory_order_acq_rel) ;
r2=x.load(memory_order_relaxed) ;
This time , no results of r1==0 && r2 == 0 happen , that memory_order I used is according to the website I mentioned at the start , see the statements :
memory_order_acquire: guarantees that subsequent loads are not moved before the current load or any preceding loads.
memory_order_release: preceding stores are not moved past the current store or any subsequent stores.
memory_order_acq_rel: combines the two previous guarantees
memory_order_relaxed: all reorderings are okay.
look work out ... still I do another test , I change code to :
global vars :
int r1=0,r2=0 ;
atomic<int> x{0} ;
atomic<int> y{0} ;
Thread1 :
x.store(1,memory_order_relaxed) ;
r1=y.load(memory_order_relaxed) ;
Thread2 :
y.store(1,memory_order_relaxed) ;
r2=x.load(memory_order_relaxed) ;
Confuse me is that , this test still get no results of r1==0 && r2==0 !! if this case works , why bother use memory_order_acq_rel ? or this only works in intel cpu ? other kind of cpu still need memory_order_acq_rel in x and y's store ?