Why is std::atomic much slower than volatile bool?

后端 未结 3 569
走了就别回头了
走了就别回头了 2021-01-31 03:24

I\'ve been using volatile bool for years for thread execution control and it worked fine

// in my class declaration
volatile bool stop_;

-----------------

// I         


        
3条回答
  •  故里飘歌
    2021-01-31 03:53

    My guess is that this is an hardware question. When you write volatile you tell the compiler to not assume anything about the variable but as I understand it the hardware will still treat it as a normal variable. This means that the variable will be in the cache the whole time. When you use atomic you use special hardware instructions that probably means that the variable is fetch from the main memory each time it is used. The difference in timing is consistent with this explanation.

提交回复
热议问题