What is Reader-writer lock?

A lock that admits any number of readers together but only one writer at a time, excluding everyone else while writing.

Field: Multicore & Synchronization

Japanese name: 読み書きロック

The idea

Threads that only read data cause no problem when they run at the same time. The problem arises when someone is modifying it. The lock is therefore split into a lock for reading and a lock for writing. Any number of threads can hold the read lock at once. Only one thread can hold the write lock, and while it does, no other reader or writer may enter. It suits data that is mostly read and rarely modified, such as configuration tables and routing tables.

Pitfalls

The first is starvation. If readers are favored, a writer can never enter as long as readers keep coming. If writers are favored, the opposite happens. The second is that it may not be as fast as expected. Even taking the read lock modifies a shared variable that counts the readers, and the cache line holding that variable bounces between cores. If the lock is held only briefly, this cost dominates and the lock can be slower than an ordinary mutex. The third is trying to switch to the write lock while still holding the read lock. If two threads do this at the same time, they deadlock.

Alternatives

For data that is read overwhelmingly often, there is a method in which readers take no lock at all. The writer makes a copy of the data, modifies it and switches the reference to the new version. The old version is freed once no thread is reading it. In another method, readers check a version number and retry if a modification occurred while they were reading. When the lock is held only briefly, a simple mutex is often sufficient.

Try it: questions about Reader-writer lock

What is a characteristic of a reader-writer lock?

  1. AAny number of writers may enter together, but only one reader
  2. BAny number of readers may enter together, but a writer enters alone, excluding everyone else
  3. COnly one thread may enter, whether reading or writing
Show answer and explanation

Answer: B. Any number of readers may enter together, but a writer enters alone, excluding everyone else

Concurrent reads cannot corrupt data, so a reader-writer lock lets readers run together and makes only writers exclusive. It raises parallelism for data that is read often and changed rarely, such as a configuration table. If readers keep arriving, though, a writer may never get in, so some implementations give writers priority.

FAQ

When is a reader-writer lock advantageous?

When reads are frequent and the lock is held for a relatively long time. If it is held long enough, the benefit of letting readers through together outweighs the cost of counting them.

Can any number of readers enter at once?

An implementation has an upper limit, but it is large enough not to matter in ordinary use.

Books on Multicore & Synchronization Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary