Read-write lock
medium 50 minMany readers or one writer, never both.
Watch lesson4:32
Where it shows up
The same pattern appears across everyday software.
Java ReentrantReadWriteLock
Supports reentry and downgrading, not upgrading: a reader that asks for the write lock never gets it. The order is unspecified by default, about first come in fair mode. At most 65,535 read holds.
Go sync.RWMutex
Any number of readers or one writer. A blocked Lock call excludes new readers, so the documentation forbids recursive read locking. It can be neither upgraded nor downgraded.
C++ std::shared_mutex
Shared and exclusive ownership since C++17. `std::shared_lock` and `std::unique_lock` are the two guards.
SQLite file locks
SHARED, RESERVED, PENDING and EXCLUSIVE in rollback mode: readers, one future writer beside them, the rope, and the write. In write-ahead log mode readers do not block writers.
PostgreSQL
Eight table lock modes with a conflict table. Reading takes ACCESS SHARE, dropping a table takes ACCESS EXCLUSIVE, and only ACCESS EXCLUSIVE blocks a plain SELECT. Rows have FOR SHARE and FOR UPDATE.
Linux process address space
Each process address space has `mmap_lock`, a read/write semaphore. Finer locks per memory area exist beside it.
Linux sequence locks and RCU
Sequence counters are for data that is rarely written, such as system time, with readers that retry. RCU, read-copy update, was added in the 2.5 development series for read-mostly data.
.NET ReaderWriterLockSlim
Has a third mode, upgradeable read, held by one thread at a time, beside readers.
POSIX locks on glibc
An attribute chooses the kind. Preferring readers is the default, and the manual says that writers can starve. Another kind prefers writers and forbids nested reads.
Where the shape appears
Caches of database rows, configuration files, cached DNS answers, dashboards that many people open, routing tables, access lists. Balances or feeds only if a whole group of values must be read together.