Read-Write locks...
-
I am working on an NT service. One of my classes is used mostly for read-only lookups into a stl map. Probably 70% of the calls made to this class are read-only requests and change no data members. The other 30% add or remove members from the intenal map object. This class is called by as many as 200 threads and is used quite a bit. Currently I have it coded so that it locks the entire class during each function call regardless of whether or not that function does an update. Anyway, I have spent a couple of hours searching the web for an open-source implementation of a read many, write once locking mechinism. I have developed a class to facilitate this and its almost done, but I thought before I bothered to finish it I would check here. the question: Does anyone know where I can get a pre-packaged Read/write locking class? Thanks, Matt Gullett PS. If I have to finish my class I will post it to Code Project.
-
I am working on an NT service. One of my classes is used mostly for read-only lookups into a stl map. Probably 70% of the calls made to this class are read-only requests and change no data members. The other 30% add or remove members from the intenal map object. This class is called by as many as 200 threads and is used quite a bit. Currently I have it coded so that it locks the entire class during each function call regardless of whether or not that function does an update. Anyway, I have spent a couple of hours searching the web for an open-source implementation of a read many, write once locking mechinism. I have developed a class to facilitate this and its almost done, but I thought before I bothered to finish it I would check here. the question: Does anyone know where I can get a pre-packaged Read/write locking class? Thanks, Matt Gullett PS. If I have to finish my class I will post it to Code Project.
Matt Gullett wrote: Does anyone know where I can get a pre-packaged Read/write locking class? I just saw one whilst flipping through Richter's Advanced Windows. I'm not near the CD now, but search MSDN, the code might be up there. He called it SWMRG (single writer multiple reader guard). --Mike-- Buy me stuff! Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines! My really out-of-date homepage Big fan of Alyson Hannigan and Jamie Salé.
-
Matt Gullett wrote: Does anyone know where I can get a pre-packaged Read/write locking class? I just saw one whilst flipping through Richter's Advanced Windows. I'm not near the CD now, but search MSDN, the code might be up there. He called it SWMRG (single writer multiple reader guard). --Mike-- Buy me stuff! Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines! My really out-of-date homepage Big fan of Alyson Hannigan and Jamie Salé.
Thanks for the reply. So far, no luck with MSDN but I am trying google now.
-
Thanks for the reply. So far, no luck with MSDN but I am trying google now.
I'm afraid you won't find anything useful on google (at least I didn't). The only good (non writer-starving) solution for Windows is the one given in the 4th edition of the Richter's book. Of course, with pthreads and condition variables it is much easier to implement RW locks; maybe you can look for a pthreads library for Windows. I vote pro drink :beer:
-
I am working on an NT service. One of my classes is used mostly for read-only lookups into a stl map. Probably 70% of the calls made to this class are read-only requests and change no data members. The other 30% add or remove members from the intenal map object. This class is called by as many as 200 threads and is used quite a bit. Currently I have it coded so that it locks the entire class during each function call regardless of whether or not that function does an update. Anyway, I have spent a couple of hours searching the web for an open-source implementation of a read many, write once locking mechinism. I have developed a class to facilitate this and its almost done, but I thought before I bothered to finish it I would check here. the question: Does anyone know where I can get a pre-packaged Read/write locking class? Thanks, Matt Gullett PS. If I have to finish my class I will post it to Code Project.
We use the very cool open source library ACE (see http://deuce.doc.wustl.edu/) for this kind of thing. If all you need is a read/write lock, it is overkill, but you might get some ideas from the source. I am sure that you can find one on the net somewhere, take a look at the links section on the C/C++ users journal web site (www.cuj.com) or cetus-links (http://www.cetus-links.com/). Since you have such a large percentage of writers, a better approach would be to use multiple maps and distribute the data evenly across the maps. For example, if your key is a string you can hash it and mod by the number of maps to figure out which one the data belongs to. Each map would have its own critical section structure which you would lock before you read or write to that map. If your data set is predictable, you can build a function that results in even distribution of the data across the maps. If your data set is not predictable, use a large number of maps and a good hash function to minimize contention. Chris Hafey
-
I am working on an NT service. One of my classes is used mostly for read-only lookups into a stl map. Probably 70% of the calls made to this class are read-only requests and change no data members. The other 30% add or remove members from the intenal map object. This class is called by as many as 200 threads and is used quite a bit. Currently I have it coded so that it locks the entire class during each function call regardless of whether or not that function does an update. Anyway, I have spent a couple of hours searching the web for an open-source implementation of a read many, write once locking mechinism. I have developed a class to facilitate this and its almost done, but I thought before I bothered to finish it I would check here. the question: Does anyone know where I can get a pre-packaged Read/write locking class? Thanks, Matt Gullett PS. If I have to finish my class I will post it to Code Project.
There was an article about this in the May 2002 issue of C/C++ User's Journal. www.cuj.com.