Back to Home

Reader-Writer Lock

Multiple readers OR one exclusive writer - never both

📚 What is a Reader-Writer Lock?

A Reader-Writer Lock allows either multiple readers to access data simultaneously, OR one exclusive writer - but never both at the same time. This optimizes for read-heavy workloads.

👓 Readers

  • • Many can read simultaneously
  • • No data corruption risk (read-only)
  • • Must wait if a writer is active

✍️ Writers

  • • Need exclusive access
  • • Block all readers and other writers
  • • Should get priority to prevent starvation

🎬 What to Watch

Notice how multiple readers can be active at once, but when a writer takes the lock, everyone else must wait!

Active Readers
0
Active Writer
0
Waiting
0
Reads Done
0
Writes Done
0

Shared Resource

Initial Value
Version: 1

Threads

Active

No active threads

Waiting Queue

No waiting threads

Lock Rules

Readers:

Multiple can read simultaneously

Writers:

Need exclusive access (no other readers or writers)

Priority:

Writers wait for readers, but get priority to prevent starvation

Event Log

Start simulation to see events...

Use Cases

  • • Database read replicas
  • • Configuration caches
  • • In-memory data stores
  • • File system access