I know, I know, the title of my message may seem provocative, since boost::mutex purposefuly do not expose lock / unlock (in order to avoid dead locks).
However the boost documentation is quite short on these aspects (to say the least), so I am asking if anyone can help me in the following use case.
Suppose you have a class Foo, which has :
- a destructor that takes some time to complete
- a method that is called by a distinct thread, but should not be called during destruction
class Foo
{
public:
virtual ~Foo()
{
//Time consuming operations here
}
//Method called by a timer belonging to a distinct class
void OnTimer()
{
//Other time consuming stuff. Should not be called during destruction !
}
};
I tried (with no success) to implement a version based on boost::mutex
//boost::mutex implementation
class Foo
{
public:
Foo()
{
}
virtual ~Foo()
{
{
boost::mutex::scoped_lock lock(mDisposingMutex);
//Time consuming operations here
}
}
//Method called by a timer belonging to a distinct class
void OnTimer()
{
{
//Imaginary code here: mutex::locked() method is private !!!
if ( ! mDisposingMutex.locked())
return;
}
//Other time consuming stuff. Should not be called during destruction !
}
private:
boost::mutex mDisposingMutex;
};
Am I totally wrong? Can anyone tell me how this is supposed to be done with boost::mutex?
Thanks !