Subj : Re: stl & threads To : comp.programming.threads From : Uenal Mutlu Date : Wed May 25 2005 04:19 am "Uenal Mutlu" wrote > "Uenal Mutlu" wrote > > > bool IsLockedByAny() const { return cLocked > 0; } > > bool IsLockedByMe() const { return cLocked > 0 && dwThrId == GetCurrentThreadId(); } > > size_t GetLockCount() const { return cLocked; } > > The above methods should be called only by current lock owner. > A safer variant to achieve this are the following replacements: > > bool IsLockedByAny() const > { > assert(dwThrId == GetCurrentThreadId()); > return cLocked > 0; > } > > bool IsLockedByMe() const > { > assert(dwThrId == GetCurrentThreadId()); > return cLocked > 0 && dwThrId == GetCurrentThreadId(); > } Fix: remove the assert in IsLockedByMe(). > size_t GetLockCount() const > { > assert(dwThrId == GetCurrentThreadId()); > return cLocked; > } > > Also the dtor can be made safer by this replacement: > ~mutex() > { > assert(cLocked == 0); > DeleteCriticalSection(&cs); > } > If it asserts in dtor then it means the lock was still held in user code, > which of course shouldn't be the case when the dtor is called. > Since assert() is executed only in Debug build the program needs > to be throughly tested in Debug build first. .