Remove ads
Synchronization primitive in computing From Wikipedia, the free encyclopedia
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.
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. 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.
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.
Several implementation strategies for readers–writer locks exist, reducing them to synchronization primitives that are assumed to pre-exist.
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
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
pthread_rwlock_t
and associated operations[10]System.Threading.ReaderWriterLockSlim
lock for C# and other .NET languages[12]std::shared_mutex
read/write lock in C++17[13]boost::shared_mutex
and boost::upgrade_mutex
locks in Boost C++ Libraries[14]SRWLock
, added to the Windows operating system API as of Windows Vista.[15]sync.RWMutex
in Go[16]std::sync::RwLock
read/write lock in Rust[18]mse::recursive_shared_timed_mutex
in the SaferCPlusPlus library is a version of std::shared_timed_mutex
that supports the recursive ownership semantics of std::recursive_mutex
.txrwlock.ReadersWriterDeferredLock
Readers/Writer Lock for Twisted[19]rw_semaphore
in the Linux kernel[20]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.
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.