Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2004-07-07 13:33:07


Peter Dimov wrote:
> compare:
>
> TryLock l( m, unlocked );
>
> if( l.try_lock() )
> {
> }
>
> with:
>
> TryLock l( m, non_blocking );
>
> if( l.locked() )
> {
> }
>

(Jumping late, forgive me if this has been discussed already.)

How about:

if( ScopedLock l = try_lock( m ) )
{
}

where try_lock is function that returns a simple wrapper class:

template< Mutex > struct try_locker
{
     Mutex & m;
     ...
};

template< Mutex > try_locker< Mutex > try_lock( Mutex & m )
{
     return try_locker< Mutex >( m );
};

and ScopedLock is some lockable type which has a constructor that
accepts a try_locker. (It would also need a bool-ish conversion to allow
the lock to be declared in the "if" statement, but that's not relevant
to this discussion.)

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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