title: How does a read-write mutex/lock work?
body: <p>Let's say I'm programming in a threading framework that does not have <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Readers-writer_lock" rel="noreferrer">multiple-reader/single-writer mutexes</a>. Can I implement their functionality with the following:</p>
  
  <p>Create two mutexes: a recursive (lock counting) one for readers and a binary one for the writer.</p>
  
  <p>Write:</p>
  
  <ul>
  <li>acquire lock on binary mutex</li>
  <li>wait until recursive mutex has lock count zero</li>
  <li>actual write</li>
  <li>release lock on binary mutex</li>
  </ul>
  
  <p>Read:</p>
  
  <ul>
  <li>acquire lock on binary mutex (so I know the writer is not active)</li>
  <li>increment count of recursive mutex</li>
  <li>release lock on binary mutex</li>
  <li>actual read</li>
  <li>decrement count of recursive mutex</li>
  </ul>
  
  <p>This is not homework. I have no formal training in concurrent programming, and am trying to grasp the issues. If someone can point out a flaw, spell out the invariants or provide a better algorithm, I'd be very pleased. A good reference, either online or on dead trees, would also be appreciated.</p>