Readers–writer lock
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.
- Read-preferring RW locks allow for maximum concurrency, but can lead to write-starvation if contention is high. This is because writer threads will not be able to acquire the lock as long as at least one reading thread holds it. Since multiple reader threads may hold the lock at once, this means that a writer thread may continue waiting for the lock while new reader threads are able to acquire the lock, even to the point where the writer may still be waiting after all of the readers which were holding the lock when it first attempted to acquire it have released the lock.
- Write-preferring RW locks avoid the problem of writer starvation by preventing any new readers from acquiring the lock if there is a writer queued and waiting for the lock. The writer will then acquire the lock as soon as all readers which were already holding the lock have completed.[3] The downside is that write-preferring locks allows for less concurrency in the presence of writer threads, compared to read-preferring RW locks. Also the lock is less performant because each operation, taking or releasing the lock for either read or write, is more complex, internally requiring taking and releasing two mutexes instead of one.[3][4] This variation is sometimes also known as "write-biased" readers-writer lock.[5][6]
- Unspecified priority RW locks does not provide any guarantees with regards read vs. write access. Unspecified priority can in some situations be preferable if it allows for a more efficient implementation.
Implementations
- POSIX standard
pthread_rwlock_t
and associated operations[7] - C language Win32 multiple-reader/single-writer lock used in Hamilton C shell.[1][4] The Hamilton lock presumes contention is low enough that writers are unlikely to be starved,[8] prompting Jordan Zimmerman to suggest a modified version to avoid starvation.[3]
- ReadWriteLock[9] interface and the ReentrantReadWriteLock[10] locks in Java version 5 or above
- Microsoft
System.Threading.ReaderWriterLockSlim
lock for C# and other .NET languages[11] std::shared_mutex
read/write lock in C++14boost::shared_mutex
andboost::upgrade_mutex
locks in Boost C++ Libraries[12]- Pseudo-code implementation in the Readers-writers problem article
- Phase fair reader-writer lock, which alternates between readers and writers[13][14]
See also
- Semaphore (programming)
- Mutual exclusion
- Scheduler pattern
- Balking pattern
- File locking
- Lock (computer science)
References
- ↑ 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.
- ↑ "Practical lock-freedom" by Keir Fraser 2004
- ↑ 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.0 4.1 Nicole Hamilton (19 October 1999). "Single-writer Multi-Reader lock for Win98". Newsgroup: comp.programming.threads. Retrieved 17 May 2011.
- ↑ "ReaderWriterLock Alternative" an open source C# implementation of a write-biased readers-writer lock
- ↑
java.util.concurrent.locks.ReentrantReadWriteLock
Java readers-writer lock implementation offers a "fair" mode - ↑ "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.
- ↑ 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
- ↑
java.util.concurrent.locks.ReadWriteLock
- ↑
java.util.concurrent.locks.ReentrantReadWriteLock
- ↑ "ReaderWriteLockSlim Class (System.Threading)". Microsoft Corporation. Retrieved 14 May 2011.
- ↑ Anthony Williams. "Synchronization – Boost 1.52.0". Retrieved 31 Jan 2012.
- ↑ "Reader-Writer Synchronization for Shared-Memory Multiprocessor Real-Time Systems" (PDF).
- ↑ "Phase Fair and Standard Reader Writer Locks".