Writer-Preferring Read-Write Lock for a Shared Device Registry
Last updated: November 29, 2026
Quick Overview
Implement a read-write lock that lets many readers hold it at once and one writer exclusively, where a waiting writer blocks new readers so writers cannot starve. Use it to protect a process-wide registry of GPU device properties.
NVIDIA
November 29, 20260
0
0 solved
Implement a read-write lock that lets many readers hold it at once and one writer exclusively, where a waiting writer blocks new readers so writers cannot starve. Use it to protect a process-wide registry of GPU device properties.
A concurrency question in NVIDIA onsites for driver, runtime and CUDA tooling teams, where device and context tables are read constantly but updated on hot-plug, MIG reconfiguration or reset. The interviewer checks condition-variable discipline, correct wait loops, and an honest discussion of starvation in both directions.
What the Interviewer Expects
- Guards all state with one mutex and waits on a condition variable in a while loop
- Tracks active readers, an active writer flag, and a count of waiting writers
- Blocks new readers while any writer is waiting
- Notifies waiters when the last reader leaves and when a writer releases
- Discusses reader starvation, fairness alternatives, and lock upgrade deadlocks
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Can a thread holding the read lock upgrade to a write lock? What goes wrong?
- How would you make it fair (FIFO between readers and writers)?
- When would you avoid a lock entirely and use RCU or a seqlock instead?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
API: acquire_read, release_read, acquire_write, release_write. Invariants: either any number of readers or exactly one writer. Writer preference: once a writer is waiting, new readers wait. No...
High-Level Design
One Condition over a mutex protecting three fields: readers, writer_active, writers_waiting. Readers wait while a writer is active or waiting. Writers announce themselves by incrementing `writ...