jeudi 13 août 2020

Why "memory_order_relaxed" treat as "memory_order_seq_cst" in my system [C++]

My Code :

std::atomic<int> x(22) , y(22);
int temp_x = -1, temp_y = -1;

void task_0(){
      x.store(33, std::memory_order_relaxed);
      temp_y = y.load(std::memory_order_relaxed);
}

void task_1(){
      y.store(33, std::memory_order_relaxed);
      temp_x = x.load(std::memory_order_relaxed);
}

int main(){
      std::thread t1(task_0);
      std::thread t2(task_1);

      t1.join();
      t2.join();

      std::cout<<temp_x<<" : "<<temp_y<<"\n";

return 0;
}

The problem is that as I use "memory_order_relaxed" So after testing 100 times one of my output should be " 22 : 22 " but my program gives :

Output :

  "33 : 33"
  "22 : 33"
  "33 : 22"

but it not gives "22 : 22" output

I tested this program in my 64 bit 2.9 GHz Quad-Core Intel Core i7 architecture. So guys what's wrong with my program, is there something that I need to understand ?

Aucun commentaire:

Enregistrer un commentaire