In a multi-threaded C++ app, do I need a mutex to protect a simple boolean?

后端 未结 5 1216
梦如初夏
梦如初夏 2020-12-15 22:54

I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library. I\'m planning to kick off OSG\'s render loop as a separate thread using boost::threa

5条回答
  •  长情又很酷
    2020-12-15 23:31

    As for C++11 and later it is finally threads-aware and clearly states that modifying a bool (or other non-atomic variable) in one thread and accessing it at the same time in another one is undefined behavior. In you case using std::atomic should be enough to make your program correct, saving you from using locks.
    Do not use volatile. It has nothing to do with threads. For more discussion look at Can I read a bool variable in a thread without mutex?

提交回复
热议问题