Readers–writer lock explained

In computer science, a readers–writer (single-writer lock,[1] a multi-reader lock,[2] a push lock,[3] or an MRSW lock) is a synchronization primitive that solves one of the readers–writers problems. An RW lock allows concurrent access for read-only operations, whereas 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 and 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.

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. http://www.boost.org/doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts.upgrade_lockable Upgrading a lock from read-mode to write-mode is prone to deadlocks, 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. The deadlock can be avoided by allowing only one thread to acquire the lock in "read-mode with intent to upgrade to write" while there are no threads in write mode and possibly non-zero threads in 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.

Implementation

Several implementation strategies for readers–writer locks exist, reducing them to synchronization primitives that are assumed to pre-exist.

Using two mutexes

Raynal 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, (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:

Initialize

Begin Read

End Read

Begin Write

End Write

This implementation is read-preferring.[6]

Using a condition variable and a mutex

Alternatively an RW lock can be implemented in terms of a condition variable,, an ordinary (mutex) lock,, and various counters and flags describing the threads that are currently active or waiting.[7] [8] [9] 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

End Read

Begin Write

End Write

Programming language support

Alternatives

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.

See also

Notes and References

  1. Hamilton, Doug . Suggestions for multiple-reader/single-writer lock? . 21 April 1995 . comp.os.ms-windows.nt.misc . hamilton.798430053@BIX.com . 8 October 2010.
  2. http://www.cl.cam.ac.uk/TechReports/UCAM-CL-TR-579.pdf "Practical lock-freedom"
  3. Web site: Push Locks – What are they?. 2009-09-02. Ntdebugging Blog. MSDN Blogs. 11 May 2017.
  4. Book: Advanced Programming in the UNIX Environment . W. Richard . Stevens . W. Richard Stevens . Stephen A. . Rago . Addison-Wesley . 2013 . 409.
  5. Java readers–writer lock implementation offers a "fair" mode
  6. Book: Raynal, Michel . Concurrent Programming: Algorithms, Principles, and Foundations . Michel Raynal . Springer . 2012.
  7. Book: The Art of Multiprocessor Programming . Maurice . Herlihy . Nir . Shavit . Elsevier . 2012 . 184–185.
  8. Book: PThreads Programming: A POSIX Standard for Better Multiprocessing . registration . Bradford . Nichols . Dick . Buttlar . Jacqueline . Farrell . O'Reilly . 1996 . 84–89. 9781565921153 .
  9. Book: Butenhof, David R. . Programming with POSIX Threads . Addison-Wesley . 1997 . 253–266.
  10. Web site: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition: pthread_rwlock_destroy. The IEEE and The Open Group. 14 May 2011.
  11. Web site: ReaderWriteLockSlim Class (System.Threading). Microsoft Corporation. 14 May 2011.
  12. Web site: New adopted paper: N3659, Shared Locking in C++—Howard Hinnant, Detlef Vollmann, Hans Boehm. Standard C++ Foundation.
  13. Web site: Synchronization – Boost 1.52.0. Anthony Williams. 31 January 2012.
  14. Book: Alessandrini, Victor . Shared Memory Application Programming: Concepts and Strategies in Multicore Application Programming . Morgan Kaufmann . 2015.
  15. Web site: The Go Programming language – Package sync. 30 May 2015.
  16. Web site: Reader–Writer Synchronization for Shared-Memory Multiprocessor Real-Time Systems.
  17. Web site: std::sync::RwLock – Rust. 26 October 2019.
  18. Web site: Readers/Writer Lock for Twisted. GitHub. 28 September 2016.
  19. Web site: Synchronization primitives in the Linux kernel: Reader/Writer semaphores. Linux Insides. 8 June 2023.