~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Deadlock and lock ordering
The four conditions and how to break one.
Main path: 4 problems, about 60 min.
Notes
The four Coffman conditions, which must all hold: mutual exclusion, hold-and-wait, no preemption, circular wait. Break any one of them.
Standard fix: a global lock order.
first, second = sorted((a, b))
with locks[first], locks[second]:
...
Other options: acquire(timeout=...) and back off, or a single lock (simple, but it serializes everything).
Gotchas: a -> b and b -> a transfers at once is the textbook deadlock. In the drills a deadlock shows up as a timeout.
5 problems
Concurrency
Locks Mutexes, lock ordering, readers and writers.
Deadlock and lock ordering guide
- Take several locks without deadlock basics py · c++ · java easy
- Shared party playlist py · c++ · java easy
- Deadlock-free bank transfers py · c++ · java easy
- Dining philosophers without deadlock py · c++ · java medium
More practice: 1 problem
- Deadlock detector 2 levels py · c++ · java medium