|This article needs additional citations for verification. (December 2010)|
In computer science, a readers-writer or shared-exclusive lock (also known as the multiple readers / single-writer lock or the multi-reader lock, or by typographical variants such as readers/writers lock) is a synchronization primitive that solves one of the readers-writers problems. A readers-writer lock is like a mutex, in that it controls access to a shared resource, allowing concurrent access to multiple threads for reading but restricting access to a single thread for writes (or other changes) to the resource. A common use might be to control access to a data structure in memory that can't be updated atomically and isn't valid (and shouldn't 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.
One potential problem with a conventional RW lock is that it can lead to write-starvation if contention is high enough, meaning that as long as at least one reading thread holds the lock, no writer thread will be able to acquire 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. To avoid writer starvation, a variant on a readers-writer lock can be constructed which prevents any new readers from acquiring the lock if there is a writer queued and waiting for the lock, so that the writer will acquire the lock as soon as the readers which were already holding the lock are finished with it. The downside is that it's 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. This variation is sometimes known as a "write-preferring" or "write-biased" readers-writer lock.
A read/write lock pattern or simply RWL is a software design pattern that allows concurrent read access to an object but requires exclusive access for write operations. In this pattern, multiple readers can read the data in parallel but an exclusive lock is needed while writing the data. When a writer is writing the data, readers will be blocked until the writer is finished writing.
- The POSIX standard
pthread_rwlock_tand associated operations.
- The C language Win32 multiple-reader/single-writer lock used in Hamilton C shell. The Hamilton lock presumes contention is low enough that writers are unlikely to be starved, prompting Jordan Zimmerman to suggest a modified version to avoid starvation.
- The ReadWriteLock interface and the ReentrantReadWriteLock locks in Java version 5 or above.
- The Microsoft
System.Threading.ReaderWriterLockSlimlock for C# and other .NET languages.
boost::shared_mutexread/write lock in the Boost C++ Libraries.
- A pseudo-code implementation in the Readers-writers problem article.
- The phase fair reader-writer lock by Brandenburg and Anderson which alternates between readers and writers.
- Semaphore (programming)
- Mutual exclusion
- Scheduler pattern
- Balking pattern
- File locking
- Lock (computer science)
- Hamilton, Doug (21 April 1995). "Suggestions for multiple-reader/single-writer lock?". comp.os.ms-windows.nt.misc. Web link. Retrieved 8 October 2010.
- "Practical lock-freedom" by Keir Fraser 2004
- Jordan Zimmerman (21 October 1999). "Single-writer Multi-Reader lock for Win98". comp.programming.threads. Web link. Retrieved 17 May 2011.
- Nicole Hamilton (19 October 1999). "Single-writer Multi-Reader lock for Win98". comp.programming.threads. Web link. Retrieved 17 May 2011.
- "ReaderWriterLock Alternative" an open source C# implementation of a write-biased readers-writer lock
java.util.concurrent.locks.ReentrantReadWriteLockJava 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". comp.programming.threads. Web link. "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". Retrieved 7 October 2011.
- "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".
- "Phase Fair and Standard Reader Writer Locks".