Subj : Re: Futexes are wrong! (Are they?) To : comp.programming.threads From : Maciej Sobczak Date : Tue Jun 07 2005 11:18 pm David Schwartz wrote: > 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. Agreed. > What about if you have ten things > to do that don't require the lock? I keep the lock as short as possible (but not shorter), moving unrelated stuff outside of the scope of the lock - this is obvious. The only problem (again about purity - I know, it's a matter of perspective ;) ) is the fact that signalling is logically related to the stuff that I did while holding the lock (after all, I want to signal that I did that stuff). The scope cleanly shows this relationship: { scoped_lock lock(mtx); // ... cond.signal(); } Above, everything that is inside the block is logically related and exists together. Of course, the scope does not contain any unrelated stuff and it can be artificially introduced as nested scope inside another block. If we try to signal after unlocking the mutex, then the logical relationship between the stuff that requires locking and signalling that it was actually done, is "lost" in the source code: { scoped_lock lock(mtx); // ... } cond.signal(); Above, it is not clear that signalling is related to what was done inside the block. I understand the performance issue, now the question is about what programming style we should promote to benefit from potential performance gain without surprising those who happen to read the code. -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/ .