【Design Pattern】Concurrency - Read Write Lock Pattern

Posted by 西维蜀黍 on 2021-07-19, Last Modified on 2022-12-10

Readers–writer Lock

In computer science, a readers–writer (single-writer lock, a multi-reader lock, a push lock, 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, 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.

Reference