
Eric Niebler wrote:
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.)
Interesting idea. I suppose you could even do this: if (ScopedLock l(m, locker())) { } where locker() is a function object that defines operator()(LockType& l) which the lock calls in the constructor, passing *this. You could have locker(), try_locker(), timed_locker(t), etc. Mike