Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-07-15 08:03:36


Matt Hurd wrote:
>
> Yes, that is the classic solution but it never strikes me as quite
> what I need.
>
> I've been struggling to find a cute solution I'm happy with. Mainly
> because I also typically want to expose the foo1_impl() for the cases
> when I wish to lock externally for multiple operations.

Andrei Alexandrescu's suggestion is to make foo1_impl take a lock:

void foo1()
{
    scoped_lock<> lock(mx_);
    foo1( lock );
}

void foo1( scoped_lock<> & l )
{
    assert( l.locked() );
    assert( l.mutex() == &mx_ ); // or not, to allow ext. sync

    // do things, incl. foo2( l );
}

This doesn't solve the boilerplate problem, though. Maybe something like

void foo1( scoped_lock<> * pl = 0 )
{
    assert( pl == 0 || pl->locked() );
    scoped_lock<> internal_lock( mx_, pl == 0 );
    if( pl == 0 ) pl = &internal_lock;

    // do things, incl. foo2( pl );
}

where the boilerplate can be hidden in a class.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk