A write-preferring readers–writer lock allows concurrent readers but gives waiting writers priority. Once a writer is waiting, new readers stop acquiring the lock, preventing readers from repeatedly delaying the writer. section 5.1.4
Why read preference can fail
Imagine two reader goroutines repeatedly acquiring a read lock, sleeping for one second, printing “Read done,” and unlocking. A writer that calls WriteLock() may remain blocked while readers continue arriving; the output repeats “Read done” indefinitely without “Write finished.” This is writer starvation. section 5.1.4
The lock tracks active readers, waiting writers, and whether a writer is active. A condition variable paired with a mutex suspends goroutines until those state conditions change. section 5.1.4
Go’s sync.RWMutex follows this write-preferring behavior: a blocked Lock call excludes new readers until the existing readers release their locks. A short run can therefore produce “Read done,” “Read done,” and then “Write finished.” section 5.1.4
