Subj : Re: Deadlock theorem To : comp.programming.threads From : Uenal Mutlu Date : Wed May 04 2005 01:41 am "Uenal Mutlu" wrote > "David Schwartz" wrote > > "Uenal Mutlu" wrote > > > > >> Your hierarchy does not allow you to relock o2 while you still hold > > >> the o1 lock. So this is not legal. > > > > > No, Sir. This scenario I'm using so often that it is "natural" for me. > > > That is: the theorem covers this and it is heavily used in how I use > > > locking in applications. > > > I wonder why you think this should not be possible? I'd suggest > > > to take a look again at the f1() and f2() functions of my prev posting. > > > It is exactly the same scenario like yours above, and it works. > > > > Your theorem states: > > > > c) If there are multiple threads then the set of objects to lock within > > a thread must not contain the same object more than once. > > (Unlocking all in the set and starting over again (either the same > > or a > > different set of objects), either in the same block or in a > > different block, is ok.) > > > > This prohibits locking o2 again while the o1 lock is still held. In this > > case, only part of the set has been unlocked, not all of it. > > Good observation, yes, rule c is not well or completely formulated :-( > But the point should be clear. Rule c should exclude this case, or a > rule d should be formulated. But since english is not my native language > it's not easy to find the right formulation to describe this case. Any idea? :-) Nope. Studying rule c revealed that it already covers this case as was already demonstrated in the following example. If o2 has to be locked twice then the second o2.Lock() says implicity that it happens in a new block. See the "another block" comments below. thread1: o3.Lock(); // Lock order is important (see rule a); all threads must use "the same direction" o2.Lock(); o1.Lock(); o2.Unlock(); // Unlock order is irrelevant (see rule b) o3.Unlock(); o1.Unlock() thread2: o2.Lock(); o1.Lock(); o2.Unlock(); o1.Unlock(); // another block (scope or func); see rule c o2.Lock(); o1.Lock(); o2.Unlock(); o1.Unlock(); // another block (scope or func); see rule c o3.Lock(); o2.Lock(); o3.Unlock(); o2.Unlock(); .