Boost Threading Multiple Reader / Single Writer Usage Questions
I am working in legacy code and I have a list collection of pointers that is accessed by multiple threads. I am having some thread locking issues with it. Mostly the list is read and very few changes (adds or deletes) are made to the list. I thought this would be a great opportunity to use a Reader Writer Lock (RWL). It seems that the shared_mutex is the Boost RWL. There are a few requirements that I am not sure shared_mutex will do for me: 1. There could be nested locks and the RWL should handle unwinding them correctly. I see that recursive_mutex seems to handle that. Does shared_mutex handle that as well? 2. Is there a way to give a higher priority to writes? I am worried about the readers starving the writers, so I would like any pending writers to be given preference over pending readers. How can I get this functionality from the Boost thread library? If I need to build my own, what class do you recommend using as a base to build from?
Brian Nantz
1. There could be nested locks and the RWL should handle unwinding them correctly. I see that recursive_mutex seems to handle that. Does shared_mutex handle that as well?
shared_mutex does not support nested locks by the same thread.
2. Is there a way to give a higher priority to writes? I am worried about the readers starving the writers, so I would like any pending writers to be given preference over pending readers.
If there are pending writers then new readers are queued. When the current lock is released (whether reader or writer) then writers + readers compete for the lock.
How can I get this functionality from the Boost thread library? If I need to build my own, what class do you recommend using as a base to build from?
You may actually better NOT using a read/write lock. The lock itself is a point of contention, and for many short-lived readers (the natural use of a read/write lock) the time locking/unlocking may be a significant portion of the overall execution time, especially on multi-core and multi-processor systems. Joe Duffy posted some timings on his blog: http://www.bluebytesoftware.com/blog/2009/02/21/AMoreScalableReaderwriterLoc... Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
participants (2)
-
Anthony Williams
-
Brian Nantz