Your read-write lock can starve the writer
transcript
A price list. Everyone reads it, all day long. One person needs to change it. Can they ever get in?
A read-write lock lets any number of readers in together. A writer waits for all of them, then runs alone.
Three readers are inside. A writer arrives and waits. Another reader arrives. If readers can always join, it just goes in. Keep that up, and the count never reaches zero. The writer starves.
The fix: prefer the writer. While one waits, new readers queue behind it. The writer goes first, and the late reader sees the new version.
The price is a short wait for a reader that arrives late. Readers barely notice. The prize is that the writer is never starved. It always gets a turn.
Many readers at once is a speed-up when reads take a while. Take it. But a writer must always get a turn. Build that in.
Full breakdown in the lecture.