A reader-writer lock is like a mutex, but keeps track of exclusive and shared access. It has three states:

  • unlocked
  • exclusive access from a single writer
  • and shared access from multiple readers. 1

There are a lot of subtle tradeoff between reader-writer lock implementations. Most implementations will block new readers when there is a writer waiting, to prevent writer starvation where many readers collectively keep the lock from ever unlocking and never allow writers to update the data 1

Implementations

  • Rust std::sync::RwLock<T>

Footnotes

Footnotes

  1. Chapter 1, Rust Atomics and Locks by Mara Bos (O’Reilly). Copyright 2023 Mara Bos, 978-1-098-11944-7 2