From: Russell Hind (rhind_at_[hidden])
Date: 2003-07-21 12:25:32


Peter Dimov wrote:
> To decide whether double lock/double unlock should be a programming error,
> you need to analyze the common scenarios where .lock() and .unlock() are
> used. If you often see the recurring pattern of
>
> if(l.locked()) return;
> l.lock();
>
> for example, this is an indication that throwing an exception is more
> appropriate. If, on the other hand, client code _typically_ looks like
>
> assert(!l.locked());
> l.lock();
>
> then it might be better to go the 'undefined behavior' route (which implies
> an assert in the library, at least in debug mode.)
>

Fair enough. My most common use is for automatically locking/unlocking

void function(void)
{
scoped_lock lock(themutex);
     ... do something
     lock.unlock();
         ... do some more
     lock.lock();
     ... do something else
}

with possible a while-loop around the unlock/lock code. From this, I
have seen it as a coding error if I call lock without an unlock but this
probably isn't the same for everyone.

Cheers

Russell