Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread][repost] recursive shared mutex
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2010-11-28 15:09:45


On Oct 31, 2010, at 4:49 PM, JOAQUIN M. LOPEZ MUÑOZ wrote:

> [Trying my luck again with this query.]
>
> Hi,
>
> Boost.Threads provides a recursive mutex and a shared
> mutex, but no recursive *and* shared mutex. Would it be a
> good idea to add this to the library? How hard is it to construct
> a recursive shared mutex from boost::sahred_mutex (i.e.
> adding reentrancy to boost::shared_mutex)?

Your question is somewhat vague: Are you referring to recursive exclusive ownership, recursive shared ownership, or both?

I suspect recursive exclusive ownership could be managed. However I believe a recursive shared ownership would be dangerous. I think it would have to choose between two designs, one which risked deadlock, or the other which risked exclusive ownership starvation.

Rationale:

The current shared/exclusive priority design (which is by Alexander Terekhov and I think is a good one) will cease to grant shared ownership after exclusive ownership has been requested. This allows time for the shared owners to dwindle down to zero so that the exclusive ownership can be granted.

Without this behavior, and with many shared owners, even if no one shared owner owned the lock for very long, an exclusive owner would always be waiting. Imagine if everyone kept cutting in front of you while you waited in line.

With this "fair" behavior, imagine thread A getting shared ownership. Then thread B requests exclusive ownership and blocks until A relinquishes shared ownership. Meanwhile thread A recursively requests shared ownership. Thread A blocks because no more shared ownerships are granted until thread B is granted exclusive ownership. Deadlock.

With recursive shared ownership on shared_mutex being a recipe for disaster, it might be confusing if the API offered recursive exclusive ownership on the shared_mutex.

-Howard


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