Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Upgradable RW lock  





2 Priority policies  





3 Implementation  



3.1  Using two mutexes  





3.2  Using a condition variable and a mutex  







4 Programming language support  





5 Alternatives  





6 See also  





7 Notes  





8 References  














Readerswriter lock






Magyar
Русский

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Incomputer science, a readers–writer (single-writer lock,[1]amulti-reader lock,[2]apush 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[edit]

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. [1] 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[edit]

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[edit]

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

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:

Initialize

Begin Read

End Read

Begin Write

End Write

This implementation is read-preferring.[4]: 76 

Using a condition variable and a mutex[edit]

Alternatively an RW lock can be implemented in terms of a condition variable, cond, an ordinary (mutex) lock, g, 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 num_readers_active and num_writers_waiting are zero and writer_active is false.

The lock and release operations can be implemented as

Begin Read

End Read

Begin Write

End Write

Programming language support[edit]

Alternatives[edit]

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[edit]

Notes[edit]

  1. ^ This is the standard "wait" operation on condition variables, which, among other actions, releases the mutex g.

References[edit]

  1. ^ Hamilton, Doug (21 April 1995). "Suggestions for multiple-reader/single-writer lock?". Newsgroupcomp.os.ms-windows.nt.misc. Usenet: hamilton.798430053@BIX.com. Retrieved 8 October 2010.
  • ^ "Practical lock-freedom" by Keir Fraser 2004
  • ^ "Push Locks – What are they?". Ntdebugging Blog. MSDN Blogs. 2 September 2009. Retrieved 11 May 2017.
  • ^ a b Raynal, Michel (2012). Concurrent Programming: Algorithms, Principles, and Foundations. Springer.
  • ^ Stevens, W. Richard; Rago, Stephen A. (2013). Advanced Programming in the UNIX Environment. Addison-Wesley. p. 409.
  • ^ a b java.util.concurrent.locks.ReentrantReadWriteLock Java readers–writer lock implementation offers a "fair" mode
  • ^ Herlihy, Maurice; Shavit, Nir (2012). The Art of Multiprocessor Programming. Elsevier. pp. 184–185.
  • ^ Nichols, Bradford; Buttlar, Dick; Farrell, Jacqueline (1996). PThreads Programming: A POSIX Standard for Better Multiprocessing. O'Reilly. pp. 84–89. ISBN 9781565921153.
  • ^ Butenhof, David R. (1997). Programming with POSIX Threads. Addison-Wesley. pp. 253–266.
  • ^ "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.
  • ^ java.util.concurrent.locks.ReadWriteLock
  • ^ "ReaderWriteLockSlim Class (System.Threading)". Microsoft Corporation. Retrieved 14 May 2011.
  • ^ "New adopted paper: N3659, Shared Locking in C++—Howard Hinnant, Detlef Vollmann, Hans Boehm". Standard C++ Foundation.
  • ^ Anthony Williams. "Synchronization – Boost 1.52.0". Retrieved 31 January 2012.
  • ^ Alessandrini, Victor (2015). Shared Memory Application Programming: Concepts and Strategies in Multicore Application Programming. Morgan Kaufmann.
  • ^ "The Go Programming language – Package sync". Retrieved 30 May 2015.
  • ^ "Reader–Writer Synchronization for Shared-Memory Multiprocessor Real-Time Systems" (PDF).
  • ^ "std::sync::RwLock – Rust". Retrieved 26 October 2019.
  • ^ "Readers/Writer Lock for Twisted". GitHub. Retrieved 28 September 2016.
  • ^ "Synchronization primitives in the Linux kernel: Reader/Writer semaphores". Linux Insides. Retrieved 8 June 2023.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Readers–writer_lock&oldid=1159126279"

    Categories: 
    Concurrency control
    Software design patterns
    Hidden categories: 
    Articles with short description
    Short description matches Wikidata
    Use dmy dates from May 2022
    All articles with unsourced statements
    Articles with unsourced statements from November 2015
    Articles with unsourced statements from April 2015
     



    This page was last edited on 8 June 2023, at 12:14 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki