2021年2月5日星期五

c++ is assignment to std::atomic

I have the following scenario

// first assigned a value greater than 0  std::atomic<double> var=1;    //thread 1  while (True) {      var = 1;  }    //thread 2  if(var >0) {      //do something  }  

My question is when thread 1 doing assignment, is there a possibility thread 2 sees var other than 1? possibily some random number or zero? It seems to happen in my production build with O2 optimization(the above snippet is not the real code, O2 may optimize most away). Seems according to the standard this should never happen?

I am using c++17, gcc9.3.1

https://stackoverflow.com/questions/66073211/c-is-assignment-to-stdatomicdouble-variable-garanteed-to-be-atomic February 06, 2021 at 11:05AM

没有评论:

发表评论