What is a Read-Write Lock?
The Problem With Regular Mutexes
A regular mutex allows only one thread at a time. But reading does not modify data. If 10 threads want to read, why make 9 of them wait? They are not interfering with each other. Only writes cause trouble.
The Library Analogy
Think of a library reference book. Multiple people can read it at the same time - they just look over each others shoulders. But if someone wants to write in the margins, everyone else must leave. The writer needs the book to themselves.
The Two Rules
Rule 1: Any number of readers can be in the critical section at the same time. They do not interfere with each other.
Rule 2: Writers get exclusive access. No other readers or writers can be present. A writer blocks everyone.
When Read-Write Locks Shine
Read-write locks are worth the complexity when reads vastly outnumber writes. A configuration cache that is read 1000 times per second but updated once per hour. A routing table that is queried constantly but changes rarely.