The Read Write Lock Fairness
A read write lock allows many readers to hold it at once but gives a writer exclusive access. This boosts throughput for data read far more often than it is written, since readers no longer block each other. The hard part is deciding who wins when readers and writers compete.
A reader preferring lock lets new readers join as long as any reader holds the lock. This maximizes read throughput but can starve writers, because a steady stream of readers means a waiting writer never finds a gap to acquire the lock.
- Reader preference New readers proceed while readers hold the lock, risking writer starvation.
- Writer preference A waiting writer blocks new readers, risking reader starvation instead.
- Fair queue Threads are served roughly in arrival order, bounding both waits.
Writer preferring locks flip the problem. Once a writer is waiting, new readers are blocked behind it, which protects the writer but can stall readers if writers keep arriving. Neither extreme is fair on its own.
A fair read write lock typically uses a queue so requests are granted in arrival order, often batching consecutive readers together for throughput while still letting a queued writer eventually run. The design trades a little raw read throughput for a bounded wait, which is usually the right choice in systems that must stay responsive under mixed load.
Key idea
Read write locks must balance reader throughput against writer progress, and a fair queue grants requests in arrival order to bound starvation on both sides.