Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] mutex is locked
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2014-06-29 10:43:19


Le 26/06/14 13:46, gast128 a écrit :
> Dear all,
>
> does anybody know if there is a possibility to check if a mutex is locked?
> There is an 'owns_lock' on the lock type but not on the mutex.
>
> The main use case for me is debugging (and documentation) purposes:
>
> void MyClass::Process()
> {
> boost::unique_lock<boost::mutex> lck(m_mtx);
>
> ProcessImpl();
> }
>
>
> void MyClass::ProcessImpl()
> {
> _ASSERT(m_mtx.is_locked()); //does not exist :(
> }
>
>
No theres is no such function. I would suggest you to do the following

void MyClass::Process()
{
    boost::unique_lock<boost::mutex> lck(m_mtx);

    ProcessImpl(lck);
}

void MyClass::ProcessImpl(boost::unique_lock<boost::mutex> &lck)
{
    _ASSERT(lck.owns_lock());
    // ...
}

BTW, I use to use this idiom and I use the same function name.
  
Best,
Vicente


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net