Subj : Re: recursive mutexes To : comp.programming.threads From : Uenal Mutlu Date : Mon May 16 2005 08:36 am "David Schwartz" wrote > > "Uenal Mutlu" wrote > > > Since you don't believe me it's your turn to prove that recursive locking > > is more dangerous (your saying) than using no recursive locking. > > My point is: recursive locking is much safer than non-recursive locking. > > We've already done this over and over. Consider: > > x.Lock(); > DoSomeStuff(); > x.Unlock(); > DoSomeStuffThatTakesALongTime(); > > If the lock for x is not recursive, we know that we can safely take a > long time without stalling other threads that might want the x lock. If the > lock is recursive, we might unknowingly hold the x lock while we do the > stuff that takes a long time. You are putting operations into 2 categories: 1) operations which can be done only if the object is locked 2) operations which can be done only if the object is not locked This is unneccessary complication, and it's dangerous. My point of view is: all operations on a shared object can be done only in a locked state (#1 above). So one has to forget #2 and never assume to do #2. An object can be modified only in a locked state, otherwise wait or give up your time-slice back to the schedular. The result is: simplication, safety, speed. > Here's another one: > > x.Lock(); > while (x.IsReservedByAnotherThread()) > { > x.Unlock(); > DoOtherStuffSinceXIsNotReady(); > } > x.DoStuff(); > x.Unlock(); > > This code will deadlock if the x mutex is recursive. The other thread > can never clear its reservation because this thread might still hold the x > mutex through the entire 'while' loop. The same would happen with non-recursive locking too, wouldn't it? Besides this, it is a bad and buggy design. You never should lock an object in a scope and unlock it in a different scope. > We really mean what we're saying. Really, really. Recursive mutexes are > really bad and they really do hide serious bugs. This is simply not true. Recursive locking is a superset of non-recursive locking. Everthing possible in non-recursive locking is possible in recursive-locking too, except deadlocking himself. So then how can recursive-locking be more dangerous than non-recursive locking? This is simple basic logic. .