Subj : Re: performance of single mutex versus Read/write mutex: any numbers? discuss To : comp.programming.threads From : Howard Hinnant Date : Mon Oct 10 2005 09:41 pm In article , "Mike Winter" wrote: > If I have a small critical section to access an element of a structure > (queue) and most accesses are reads and once in a blue moon a write (PI > seconds is a nano-century!) occurs where the primary element is superceded, > but only completely superceded when its reference count is 0(no more > readers), I think a single mutex may be overkill. I would think that a > single mutex would be less performant than say a more complex structure like > say a read-write mutex. Then I think about what it takes to manage a > ReadWriteMutex (mutex + reader-count + writing). > > Consider > > struct ReadWriteMutex { > Mutex mWriterMutex; // gets grabbed rarely > AtomicCounter mReaderCount; // gets incremented/decremented securely > and fast > ReaderMutex mReaderMutex; // only blocks if mWriterMutex is locked > }; > > Thats the context/concept. > > Heres the question: Are there any numbers /papers in the ether that can > point to how the ReaderWriterMutex will compare to the ordinary Mutex? Links > welcomed > > MW This link might be helpful: http://home.twcny.rr.com/hinnant/cpp_extensions/rw_perf.html It contains demo test code which attempts to justify the use case for read/write and even read/write/upgradable locks. Timing results for a dual processor machine are presented. The link also describes a "lock reduced" mutex which avoids locking an OS-based mutex if there is no contention, although there is no code, just hand-waving. At best this paper should only convince you that perhaps it is worth the trouble to code it up both ways and measure. -Howard .