Boost logo

Threads-Devel :

From: Anthony Williams (anthony_at_[hidden])
Date: 2008-04-14 10:34:20


Quoting Frank Mori Hess <frank.hess_at_[hidden]>:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Monday 14 April 2008 03:40 am, Anthony Williams wrote:
>> sides, as you point out. This only happens if you start with an
>> upgrade_lock on A, since otherwise you have to unlock the shared lock
>> before you can take the unique lock, and taking an upgrade lock should
>
> I don't think that is true, since you can smoothly transition from a
> shared_lock to an upgrade_lock. It's true upgrade_lock doesn't support
> moving a shared_lock into an upgrade lock, but you can transition from a
> shared to upgrade lock by doing:
>
> mutex_type m;
> shared_lock<mutex_type> shared(m); //A upgrade_lock<mutex_type>
> upgrade(m); // B
> shared.unlock();

The locks on lines A and B are independent: the fact they are from the
same thread is not taken into account.

If another thread tries to take exclusive ownership between lines A
and B you might get deadlock. I wouldn't recommend holding multiple
locks on the same mutex from the same thread unless it's explicitly
documented as recursive.

Anthony

-- 
Anthony Williams            | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk