Implement a Read-Write Lock
📖 What is a read-write lock? It is a lock with two ways in. Any number of threads can hold it for reading at the same time, because reading does not change anything. A thread that wants to write must have the data to itself, so it waits until every reader has left, and nobody else gets in while it writes.
A plain mutex lets one thread in at a time, full stop. That is correct, but wasteful when most threads only read: a config table read a thousand times a second and changed once a minute should not make its readers queue up behind each other. The read-write lock is the standard answer, and building one is a favorite concurrency question because it is small, it has a real design choice in it (who goes first when readers and writers both wait), and the classic bugs (lost updates, missed wake-ups, starvation) all show up if you are careless.
Requirements
"Implement a read-write lock. Many readers should be able to hold it at once, but a writer needs it alone."
The interviewer gives you the one-line idea. What you need to find out is how it behaves when readers and writers compete, because that is where implementations differ.
Clarifying Questions
You: Can I build it on top of a normal mutex and a condition variable, or should I use the language's built-in read-write lock?
Interviewer: Build it yourself from a mutex and a condition variable. Using ReentrantReadWriteLock would make this a one-liner.
You: If a writer is waiting while readers hold the lock, may a new reader still come in?
Interviewer: No. Writes are rare but important, and we have seen writers wait forever behind a steady stream of readers. Once a writer is waiting, new readers wait too.
You: Do you want non-blocking versions, like try_acquire_read, that return straight away?
Interviewer: Yes, one for each mode. They return true if they got the lock and false otherwise, and they never wait.
You: What should happen if someone releases a lock they do not hold?
Interviewer: Don't crash. Return false and leave the state alone.
You: Does a thread need to take the read lock again while it already holds it, or upgrade from read to write?
Interviewer: Not for now. Keep it non-reentrant. We may talk about upgrades later.
You: Do we need to know which thread holds the lock?
Interviewer: No. Just count. A monitoring page wants to show how many readers are inside, whether someone is writing, and how many writers are waiting.
The second answer is the important one. It picks the policy of the lock. Without it, the simplest code lets readers in whenever no one is writing, and a busy system can then keep a writer out forever. That failure has a name, starvation, and we will see it in code below.
💡 Interview tip: For any lock or queue question, ask "who goes first when both kinds of threads are waiting?" before you write code. The answer decides your state variables. Changing the policy after the code is written usually means starting over.
Final Requirements
- Any number of threads may hold the read lock at the same time.
- Only one thread may hold the write lock, and only while no read locks are held.
- Writer preference: while a writer is waiting, new readers must wait, even if only readers hold the lock right now.
acquire_readandacquire_writeblock until the lock can be taken. Blocked threads must sleep, not spin.try_acquire_readandtry_acquire_writetake the lock if the same rules allow it right now, and otherwise return false at once without waiting.release_readandrelease_writegive the lock back and return true, or return false and change nothing if that kind of lock is not held.readers(),is_writing()andwaiting_writers()report the current state.
Out of scope
- Reentrancy (taking a lock you already hold) and upgrading a read lock to a write lock.
- Timeouts on the blocking methods.
- Tracking which thread holds the lock.
- Locks shared between processes.