Subj : Re: Challenge: Multithreading & Synchronization To : comp.programming.threads From : Uenal Mutlu Date : Thu May 19 2005 01:53 pm wrote > > Uenal Mutlu wrote: > > > >> // assume all public functions can make calls to each > other, > > > >> // and also to internal funcs > > > >> > > > >> void f1(void* Ap = 0) > > > >> { // uses vJD, vMD > > > >> Locker L1(vJD.m); // the order of the locks is > important > > > >> Locker L2(vMD.m); > > > >> > > > >> //... > > > >> } > > > >> > > > >> void f2(void* Ap = 0) > > > >> { // uses vMD, vSD > > > >> Locker L1(vSD.m); > > > >> Locker L2(vMD.m); > > > >> > > > >> //... > > > >> } > > > >> > > > >> void f3(void* Ap = 0) > > > >> { // uses vSD, vMD > > > >> Locker L1(vSD.m); > > > >> Locker L2(vMD.m); > > > >> > > > >> //... > > > >> } > > Maybe I'm missing something: > > thread 1 calls f1, locks JD, MD... > > thread 2 slips in and calls f2, locks SD, tries to lock MD, > but has to wait on thread 1. > > thread 1 continues, and, based on > > > >> // assume all public functions can make calls to each > other, > let's say f1 calls f3, thus tries to lock SD, but can't because thread > 2 has it locked, so waits on thread 2 who is waiting on thread 1... > > am I missing something? You are right. The "assume all functions can make calls to each other" is relative: additionally the deadlock theorem must be correctly applied. So it would mean a function can call any other function only if the sum of both follows the deadlock theorem, so every calling function must know whether its own locks plus the locks of the called function are conforming to the theorem. Only such functions can be called. .