Subj : Re: recursive mutexes To : comp.programming.threads From : Uenal Mutlu Date : Sun May 15 2005 06:11 pm "Uenal Mutlu" wrote > "Peter Dimov" wrote > > Uenal Mutlu wrote: > > > "David Schwartz" wrote > > >> Yes, it can and must know that it already has the lock. A > > >> function that operates on X should be defined as being called with X > > >> locked, or with X unlocked. It could even take a boolean that > > >> indicates whether the caller has locked the object or not, though > > >> this is rarely the right thing to do. > > > > > > You are unnecessarily complicating things and make the program > > > slower by doing these testings. > > > > No, he's in fact making the program faster. > > Are you sure? Let's see: > > void X::f() > { > Lock(); > for (int i = 0; i < 1000; i++) > f(); > Unlock(); > } > > void X::g() > { > if (!IsLocked()) > return; > //...do..something... > } > > Are you saying the above version of g() is faster than this one: ? > void X::g() > { > //...do..something... > } Besides being slower, the first version is also buggy. And I don't know what he or you want do if the object is not already locked. I guess you will do the following: void X::g() { bool fILockedItHere = false; if (!IsLocked()) { Lock(); fILockedItHere = true; } //...do..something... if (fILockedItHere) Unlock(); } But, this is not thread safe!!! :-) Do you know why? The only consequence is: my method is the only right one, believe me! :-) .