Subj : Re: recursive mutexes To : comp.programming.threads From : doug Date : Mon May 16 2005 09:16 am > You seem to not understand what I'm talking about. > Do you understand this fact: "Recursive locking is defined for the current > lock owning thread only"? > Since you already have the lock you can do what ever you want since nobody > else can change anything, but you can do! You simply increment it in > Lock() > and decrement it in Unlock(). Because it is safe because you already have > it locked. > > Think carefully. How is this possible? It is possible if you store 'have I got this lock?' in thread local storage. So yes, by keeping a counter here, you don't need a memory barrier in recursive Lock()/Unlock() *. However, when you find that you don't have the semaphore, you still need synchronisation between threads so that the sema4 is acquired safely. What you've ended up doing here is accessing TLS for *every* acquire - thus making the common case of 'semaphore not acquired' slower. (* not true on certain SMP architectures. Without a memory barrier in Lock()/Unlock(), a thread rescheduled on a different CPU may mis-read the recursive counter (depending on how TLS is implemented).) Time to read your next chapter... .