title: Read-write lock with only one underlying lock?
body: <p>I've written a read-write lock using Python's concurrency primitives (I think!). Every implementation I've read on SO or elsewhere seems to use 2 locks -- one for reads, and another for writes. My implementation contains only one monitor for reads, but I may be missing something crucial -- can anyone confirm that this will work? If so, what is the benefit to using an additional write lock?</p>
  
  <p>This is the classic read-write lock with preference for readers (may starve writers). I use a dummy cache to demonstrate the reading and writing.</p>
  
  <pre class="lang-py prettyprint-override"><code>    import threading as t
  
      class ReadWriteCache(object):
          def __init__(self):
              self.cache = {}
              self.reads = 0
              self.read_cond = t.Condition(t.Lock())
  
          def read(self, key):
              with self.read_cond:      # Register the read, so writes will wait()
                  self.reads += 1
  
              result = self.cache[key]
  
              with self.read_cond:
                  self.reads -= 1
                  if not self.reads:
                      self.read_cond.notify_all()
                  return result
  
          def update(self, key, value):
              with self.read_cond:
                  while self.reads:
                      self.read_cond.wait()   # Wait for reads to clear
                  self.cache[key] = value     # With read lock, update value
  </code></pre>