Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read-Write locks...

Read-Write locks...

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++algorithmsannouncement
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Gullett
    wrote on last edited by
    #1

    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.

    M C J 3 Replies Last reply
    0
    • M Matt Gullett

      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.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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é.

      M 1 Reply Last reply
      0
      • M Michael Dunn

        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é.

        M Offline
        M Offline
        Matt Gullett
        wrote on last edited by
        #3

        Thanks for the reply. So far, no luck with MSDN but I am trying google now.

        N 1 Reply Last reply
        0
        • M Matt Gullett

          Thanks for the reply. So far, no luck with MSDN but I am trying google now.

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          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:

          1 Reply Last reply
          0
          • M Matt Gullett

            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.

            C Offline
            C Offline
            Chris Hafey
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • M Matt Gullett

              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.

              J Offline
              J Offline
              JT Anderson
              wrote on last edited by
              #6

              There was an article about this in the May 2002 issue of C/C++ User's Journal. www.cuj.com.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups