Subj : Re: Lockable objects To : comp.programming.threads From : Uenal Mutlu Date : Sat May 21 2005 03:31 pm "Peter Dimov" wrote > Uenal Mutlu wrote: > > > You said it: diagnostics, and debug. > > Esp. in such situations like below: > > > > void f() > > { // requires that object must be locked before calling this func > > > > assert(!m.IsLocked()); > > > > //... > > } > > This makes sense as long as IsLocked() only returns true if the current > thread is the one holding the lock. This in effect requires an owner field. True, actually IsLocked() returns true if both is true. A better method would be to have: bool IsLocked(); // return true if mutex locked by anybody bool IsLockedByMe(); // return true if locked and the current thread is the lock owner void f() { // requires that object must be locked before calling this func assert(!m.IsLockedByMe()); //... } > Another approach that works on application level is to enforce the use of > lock classes (such as boost::scoped_lock) and pass such a lock to f. > > void f( scoped_lock & lock ) > { > assert( lock.locked() ); > assert( lock.mutex() == &m ); > > // ... > } Yep. .