Christian Henning wrote:
Hi there, I'm still working on the transistion from an old thread lib to the boost thread lib.
I have two threads, the GUI thread and a worker thread. The worker thread is always inside a loop that only breaks if someone set the flag.
The GUI thread has from time to time to stop the worker thread. This is currently done by setting a flag ( _bStop ) and the worker thread will eventually stop like:
void run() { boost::mutex::scoped_lock oLock( _oRunMutex );
do { ...
if( _bStop ) _oRunCondition.wait( oLock );
} while( _bTerminate == false ); }
You must use "while" rather than "if" to test the condition associated with a condition variable because of the possibility of spurious wake-ups (and in some cases the possibility of the condition becoming false by the time the thread reacquires the mutex). Also you should not use an initial underscore in the names of global variables. It's not clear from this code whether you're using such names for global or class member variables.
I believe that this implemention can lead to deadlocks. But how can I implement the behavior that the GUI thread will break until the worker thread has stopped.
Use the thread::join function. Ben.