Tip

Reader-writer monitors were not used during my semester in CPSC 213. They’re significantly less relevant than the standard mutex, at least when it comes to the frequency with which they appear in the course material.

Reader writer monitors are a type of mutex that enables concurrent reads while enforcing exclusive access on writes.

These mutexes have three possible states instead of two. they can be free, held for reading or held for writing. If held for reading, multiple readers can access sections protected by the mutex simultaneously.

Operations

  • mutex_lock(): lock for writing
    • only acquires lock if it is free
    • Sets state to held for writing
  • mutex_lock_read_only():
    • If lock is free, set its state to held for reading
    • increments a reader count (or set)
  • mutex_unlock():
    • if held for writing set state to free
    • if held for reading: decrement reader count
      • Set state to free if count is zero

Policy

There is some degree of change when it comes to the specific implementation of reader-writer monitors in regards to the precise policy they use. For example, should the monitor disallow new readers while the writer is waiting? Or the opposite? The answer depends on the application, and there is no right answer.