According to intel's manual. Neither Loads Nor Stores Are Reordered with Like Operations According to 8.2.3.2 Neither Loads Nor Stores Are Reordered with Like Operations
at document https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html enter image description here
but I created a simple case, I found r1=1 and r2=2 happened.
#include <thread>
#include <iostream>
using namespace std;
volatile int x;
int b[500];
volatile int y;
volatile int start;
int s1;
int s2;
int s3;
int s0;
int foo()
{
while(start==0);
x=1;
asm volatile("" ::: "memory");
y=1;
return 0;
}
int fool2()
{
int a,b;
while(start==0);
a=x;
asm volatile("" ::: "memory");
b=y;
if(a==0 && b==1)
s0++;
if(a==0 && b==0)
s1++;
if(a==1 && b==0)
s2++;
if(a==1 && b==1)
s3++;
return 0;
}
int main()
{
int i=0;
while(1)
{
x=y=0;
thread t1(foo);
thread t2(fool2);
start = 1;
t1.join();
t2.join();
i++;
if((i&0xFFFF)==0)
{
cout<<s0<<" "<<s1<<" "<<s2<<" "<<s3<<endl;
}
}
}
g++ -O2 -pthread e.cpp
gcc version 7.5.0
output:
69 86538 1 19246512
The four case (r1 and r2 with 0, 1 combination) is all possible.