- Readers-writer lock
In
computer science , a readers-writer lock (also known by the name multi-reader lock , or by typographical variants such as readers/writers lock) is a synchronization primitive that solves one of thereaders-writers problem s. A readers-writer lock is like amutex , in that it controls access to some shared memory area, but it allows multiple threads to read from the shared area concurrently. Any thread that needs to "write" to the shared memory, of course, needs to acquire an exclusive lock.One potential problem with a conventional RW lock is that it can lead to write-starvation, meaning that so 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 finished their work in the shared area and 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. This variation is sometimes known as a "write-preferring" or "write-biased" readers-writer lock.
Readers-writer locks are usually constructed on top of
mutex es andcondition variable s, or on top of semaphores. They are rarely implemented from scratch.ee also
*
Read/write_lock_pattern
Wikimedia Foundation. 2010.