Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::recursive_mutex doesn't lock ???
From: Marsh Ray (marsh_at_[hidden])
Date: 2010-11-02 16:22:01


On 11/02/2010 02:33 PM, Chris wrote:
>
> So far no problem, but if I use recursive_mutex instead...
>
> Scenario 2: (NOT OK)
>
> int main()
> {
> boost::recursive_mutex io_mutex;
> io_mutex.lock();
>
> // thread will NOT be blocked ???
> boost::recursive_mutex::scoped_lock lock(io_mutex);
> cout<< "Test" // this statement is executed ???
> }
>
> no blocking occurs??

Yep, that's what the 'recursive' means: the same thread is allowed to
re-acquire the lock multiple times. After all, if this thread already
has acquired a lock on the mutex it's basically a no-op. The resource is
protected from concurrent access.

If a thread blocks on a non-recursive mutex that it's already locked,
wouldn't it be a guaranteed deadlock?

Some code might have its reasons, but why would you want a thread to
block in the usual case?

Just depends on what kind of mutexes you're more familiar with I guess.

> How does one lock a recursive_mutex then?

If you want to observe something blocking, create another thread.

- Marsh


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