Subj : Re: Futexes are wrong! (Are they?) To : comp.programming.threads From : David Schwartz Date : Tue Jun 07 2005 10:49 am "Maciej Sobczak" wrote in message news:d84eig$8qt$1@sunnews.cern.ch... > Interestingly, the scoped lock pattern (realized with the RAII idiom in > C++ and native in languages that support the "synchronized" keyword or > alike) promotes exactly this way of working: Personally, I think the scoped lock pattern is an abomination. A lock should be held for exactly as long as needed. If this perfectly corresponds to a language scope, then great. If not, to hold the lock longer than needed because it fits a pattern, well, down that path is recursive locks and all the other such abominations. > { > scoped_lock lock(mtx); > > // do something ... > condvar.signal(); > } > > Above, condvar is signalled before the mutex gets unlocked. > Doing otherwise would require something like this: > > { > scoped_lock lock(mtx); > > // do something ... > } > condvar.signal(); > > which can become ugly in the outermost block (like when the block is the > whole funcion body). > > What about this? You should hold a lock for exactly as long as you need it. If this means a scope just for the lock, then so be it. What about if you have ten things to do that don't require the lock? Should you do them with the lock held just because this makes the block cleaner? DS .