Sunday, April 29, 2018

Implyment read write lock with mutex



Using two mutexes[edit]

Raynal demonstrates how to implement an R/W lock using two mutexes and a single integer counter. The counter, b, tracks the number of blocking readers. One mutex, r, protects b and is only used by readers; the other, g (for "global") ensures mutual exclusion of writers. This requires that a mutex acquired by one thread can be released by another. The following is pseudocode for the operations:
Begin Read
  • Lock r.
  • Increment b.
  • If b = 1, lock g.
  • Unlock r.
End Read
  • Lock r.
  • Decrement b.
  • If b = 0, unlock g.
  • Unlock r.
Begin Write
  • Lock g.
End Write
  • Unlock g.
This implementation is read-preferring.[


https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Using_two_mutexes

No comments:

Post a Comment