Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] [thread] condition_variable_any::wait and recursive_mutex
From: Anthony Williams (anthony_at_[hidden])
Date: 2010-09-06 08:21:04


On 06/09/10 11:22, Rob Moore wrote:
> Boost version 1.40. Compiler VC9.
>
> I have found an issue where condition_variable_any::wait does not fully
> unlock a recursive_mutex if its recursion count is greater than 1. Is
> this deliberate? The simple code below reproduces the issue – the
> program will deadlock.

Yes, this is deliberate. If a recursive mutex is locked recursively then
in general you don't know what the upper levels of the recursion are
doing, and what data would be in an inconsistent state if the lock was
released.

> I am aware of the adopt_lock argument to the lock constructor, but that
> doesn’t work if the thread hasn’t already locked the mutex. What I think
> I need is one of 2 things:
>
> 1. A wait function on conditional_variable_any that will completely
> unlock and then relock a recursive_mutex. This would be ideal.

If you really need it, write a recursive_mutex wrapper that counts how
many times it was locked, and pass that to condition_variable_any.

> 2. A new argument to the lock constructor that has the same behaviour as
> adopt_lock if the thread already has the mutex. If the thread does not
> have the mutex the new constructor will locks the mutex (same as the
> basic constructor with no extra argument). This would allow me to have a
> class with multiple methods that can call each other and where any
> method could be the first one to be called in any given thread.

You could just have each public member function lock the mutex and then
forward to a private member function which can thus freely call other
private member functions.

Anthony

-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

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