Readers–writer lock

RW locks can be designed with different priority policies for reader vs. writer access.

Several implementation strategies for readers–writer locks exist, reducing them to synchronization primitives that are assumed to pre-exist.

Raynal demonstrates how to implement an R/W lock using two mutexes and a single integer counter.

One mutex, r, protects b and is only used by readers; the other, g (for "global") ensures mutual exclusion of writers.

[7][8][9] For a write-preferring RW lock one can use two integer counters and one Boolean flag: Initially num_readers_active and num_writers_waiting are zero and writer_active is false.

The Linux kernel implements a special solution for few writers called seqlock.