~/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.
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.
4 problems
Concurrency
Locks Mutexes, lock ordering, readers and writers.
Deadlock and lock ordering
- Basics: take several locks without deadlock basics easy
- Shared party playlist easy
- Dining philosophers without deadlock medium
- Deadlock-free bank transfers easy