Imagine you have a little calculation method, which is startet by a thread:
boost::mutex mMutex;
void MyClass::DoSomething {
boost::unique_lock<boost::mutex> tLock(mMutex);
if(tLock.owns_lock() {
// do some stuff...
}
}
And you want to start this in a thread, raised from different member functions. They can be called simultanous, but you can’t know when:
void MyClass::Process {
boost::thread t1(&MyClass::DoSomething, this);
// go on ...
}
void MyClass::Foo {
boost::thread t2(&MyClass::DoSomething, this);
// and something more ...
}
How to prevent t2 from beeing executed at all, if t1 is running? My unique_lock seems to fail.
Make a variable and before you start the
t1thread, increase that variable in atomic way. When it finished, decrease that variable to null in atomic way. InFooyou should just check if this variable is null, or not.Check this example.