Subj : Re: recursive mutexes To : comp.programming.threads From : David Schwartz Date : Mon May 16 2005 01:05 pm "Uenal Mutlu" <520001085531-0001@t-online.de> wrote in message news:d699ce$4m9$03$1@news.t-online.com... > "David Schwartz" wrote >> I never argued the performance issue. But here's a more realistic >> example: > The example was in response to Peter's posting who wrote "No, he's in fact > making the > program faster." > To your question regarding the speed of recursive vs. non-recursive locks: > True, recursive locking is slower than non-recursive locking. Right, so there's a performance penalty. > But recursive locking simplifies coding (no need to have a locked and > unlocked > version of a function) Simplifies? How hard is to write: void locked_f() { Lock(); f(); Unlock(); } Or even: void f(bool locked) { LockHolder lh(locked); // code goes here } > and makes the application safer wrt self-deadlocking. Actually, as I've showed many times, it makes it much less safe because the semantics of the 'unlock' function are ambiguous. > And my point is: if the recursive-locking is a fast implementation, then > the > overhead of recursive locking is minimally (inc and dec). And if this is > true (and it is) > then why make life complicated by not using them since they have the above > positive properties. Actually, it's not true. A recursive lock must first determine what thread holds the lock, and this can be an expensive operation. > And, IMO it is not a good design to test in f() whether the object > was locked or not. I prefer the following "agreement" or "contract": > It is the caller's job to call f() only after having the lock. > It is not f()'s job to check whether the object was locked. > This too simplies coding, and it leads to faster programs. I agree, however, this is exactly what using recursive mutexes does. The first thing the implementation of a recursive mutex must do is check whether the current thread holds the lock or not. DS .