Readers–writer lock


In computer science, a readers–writer is a synchronization primitive that solves one of the readers–writers problems. An RW lock allows concurrent access for read-only operations, while write operations require exclusive access. This means that multiple threads can read the data in parallel but an exclusive lock is needed for writing or modifying data. When a writer is writing the data, all other writers or readers will be blocked until the writer is finished writing. A common use might be to control access to a data structure in memory that cannot be updated atomically and is invalid until the update is complete.
Readers–writer locks are usually constructed on top of mutexes and condition variables, or on top of semaphores.

Upgradable RW lock

Some RW locks allow the lock to be atomically upgraded from being locked in read-mode to write-mode, as well as being downgraded from write-mode to read-mode. Upgradable RW locks can be tricky to use safely, since whenever two threads holding reader locks both attempt to upgrade to writer locks, a deadlock is created that can only be broken by one of the threads releasing its reader lock.

Priority policies

RW locks can be designed with different priority policies for reader vs. writer access. The lock can either be designed to always give priority to readers, to always give priority to writers or be unspecified with regards to priority. These policies lead to different tradeoffs with regards to concurrency and starvation.
Several implementation strategies for readers–writer locks exist, reducing them to synchronization primitives that are assumed to pre-exist.

Using two mutexes

demonstrates how to implement an R/W lock using two mutexes and a single integer counter. The counter,, tracks the number of blocking readers. One mutex,, protects and is only used by readers; the other, 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.
  • Increment.
  • If, lock.
  • Unlock.


End Read
  • Lock.
  • Decrement.
  • If, unlock.
  • Unlock.


Begin Write
  • Lock.


End Write
  • Unlock.

This implementation is read-preferring.

Using a condition variable and a mutex

Alternatively an RW lock can be implemented in terms of a condition variable,, an ordinary lock,, and various counters and flags describing the threads that are currently active or waiting. For a write-preferring RW lock one can use two integer counters and one boolean flag:
Initially and are zero and is false.
The lock and release operations can be implemented as

Begin Read
  • Lock
  • While or :
  • * wait,
  • Increment
  • Unlock.


End Read
  • Lock
  • Decrement
  • If :
  • * Notify
  • Unlock.


Begin Write
  • Lock
  • Increment
  • While or is :
  • * wait,
  • Decrement
  • Set to
  • Unlock.


End Write
  • Lock
  • Set to
  • Notify
  • Unlock.

Programming language support

The read-copy-update algorithm is one solution to the readers–writers problem. RCU is wait-free for readers. The Linux kernel implements a special solution for few writers called seqlock.