Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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::threads, passing a data structure containing shared state in to the thread. I'm trying to avoid anything too heavyweight (like mutexes) for synchronization, as the render loop needs to be pretty tight, and OSG itself tries to avoid having to ever lock. Most of the shared state is set before the thread is started, and never changed. I do have some data that does need to be changed, which I am planning to double-buffer. However, I have a simple boolean for signaling the thread to suspend rendering, and later resume rendering, and another to kill it. In both cases the app thread sets the bool, and the render thread only reads it. Do I need to synchronize access to these bools? As far as I can tell, the worse thing that could happen is the the render loop continues on for an extra frame before suspending or quitting.

like image 574
Brian Stewart Avatar asked Oct 21 '08 18:10

Brian Stewart


People also ask

Can multiple threads lock on a mutex?

Using Locking HierarchiesThere could be a problem if two threads attempt to claim both resources but lock the associated mutexes in different orders. For example, if the two threads lock mutexes 1 and 2 respectively, then a deadlock occurs when each attempts to lock the other mutex.

How do you use mutex?

You are supposed to check the mutex variable before using the area protected by the mutex. So your pthread_mutex_lock() could (depending on implementation) wait until mutex1 is released or return a value indicating that the lock could not be obtained if someone else has already locked it.

Is mutex thread safe?

Shared mutexes and locks are an optimization for read-only pieces of multi-threaded code. It is totally safe for multiple threads to read the same variable, but std::mutex can not be locked by multiple threads simultaneously, even if those threads only want to read a value.

Are booleans thread safe C++?

It's more of a C question then a Rust question. But, no, it's not safe. You should use atomics or mutexes for synchronization.


3 Answers

In C++11 and later, which has standards-defined concurrency, use std::atomic<bool> for this purpose. From http://en.cppreference.com/w/cpp/atomic/atomic:

If one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (see memory model for details on data races).


The following old answer may have been true at some time in the past with some compilers and some operating environments, but it should not be relied upon today:

You're right, in this case you won't need to synchronise the bools. You should declare them volatile though, to ensure that the compiler actually reads them from memory each time, instead of caching the previous read in a thread (that's a simplified explanation, but it should do for this purpose).

The following question has more information about this: C++ Thread, shared data

like image 174
Greg Hewgill Avatar answered Oct 18 '22 05:10

Greg Hewgill


Why not simply use an interlocked variable?

like image 7
shoosh Avatar answered Oct 18 '22 06:10

shoosh


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<bool> 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?

like image 4
Tadeusz Kopec for Ukraine Avatar answered Oct 18 '22 06:10

Tadeusz Kopec for Ukraine