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

后端 未结 5 1220
梦如初夏
梦如初夏 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:28

    I don't think you need a fully fledged mutex here -- though the render thread will need to busy wait in the 'suspended' state if you aren't using a synchronization object that supports a wait primitive.

    You should look into using the various interlocked exchange primitives though (InterlockedExchange under Windows). Not because read/writes from the bool are non-atomic, but to ensure that there are no weird behaviours the compiler reordering memory accesses on a single thread.

提交回复
热议问题