Boost logo

Threads-Devel :

From: Anthony Williams (anthony_at_[hidden])
Date: 2008-04-14 03:40:25


Quoting Frank Mori Hess <fmhess_at_[hidden]>:

> I'm curious if there is any theory on how to avoid deadlock when using a
> shared mutex? Having a well-defined locking order works well for an
> ordinary mutex, but things get messy with a shared mutex. For example, if
> we have mutexes A and B then the locking sequence
>
> 1) shared lock A
> 2) unique lock B
> 3) upgrade to unique lock A

My advice: don't do that. By sandwiching the lock to B within the two
halves of the lock on A, you've exposed yourself to deadlocks on both
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
be rare anyway, as there can be only one.

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