Readers–writer lock

For the psychological difficulty sometimes encountered by writers, see Writer's block.

In computer science, a readers-writer (RW) or shared-exclusive lock (also known as a multiple readers/single-writer lock[1] or multi-reader lock[2]) 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 (and should not be read by another thread) until the update is complete.

Readers–writer locks are usually constructed on top of mutexes and condition variables, or on top of semaphores.

The read-copy-update (RCU) 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.

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.

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 (read-preferring), to always give priority to writers (write-preferring) or be unspecified with regards to priority. These policies lead to different tradeoffs with regards to concurrency and starvation.

Implementations

See also

References

  1. 1.0 1.1 Hamilton, Doug (21 April 1995). "Suggestions for multiple-reader/single-writer lock?". Newsgroup: comp.os.ms-windows.nt.misc. Usenet: hamilton.798430053@BIX.com. Retrieved 8 October 2010.
  2. "Practical lock-freedom" by Keir Fraser 2004
  3. 3.0 3.1 3.2 Jordan Zimmerman (21 October 1999). "Single-writer Multi-Reader lock for Win98". Newsgroup: comp.programming.threads. Retrieved 17 May 2011.
  4. 4.0 4.1 Nicole Hamilton (19 October 1999). "Single-writer Multi-Reader lock for Win98". Newsgroup: comp.programming.threads. Retrieved 17 May 2011.
  5. "ReaderWriterLock Alternative" an open source C# implementation of a write-biased readers-writer lock
  6. java.util.concurrent.locks.ReentrantReadWriteLock Java readers-writer lock implementation offers a "fair" mode
  7. "The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition: pthread_rwlock_destroy". The IEEE and The Open Group. Retrieved 14 May 2011.
  8. Ziv Caspi (20 October 1999). "Re: Single-writer Multi-Reader lock for Win98". Newsgroup: comp.programming.threads. Retrieved 7 October 2011. Forgive me for saying so, but this implementation favors readers instead of the writer. If there are many readers, the writer will never have a chance to write
  9. java.util.concurrent.locks.ReadWriteLock
  10. java.util.concurrent.locks.ReentrantReadWriteLock
  11. "ReaderWriteLockSlim Class (System.Threading)". Microsoft Corporation. Retrieved 14 May 2011.
  12. Anthony Williams. "Synchronization – Boost 1.52.0". Retrieved 31 Jan 2012.
  13. "Reader-Writer Synchronization for Shared-Memory Multiprocessor Real-Time Systems" (PDF).
  14. "Phase Fair and Standard Reader Writer Locks".