~/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.
Lock (mutex)
One thread in a critical section at a time.
Main path: 5 problems, about 2 hours.
Notes
Recognise it when: shared mutable state, "thread-safe", check-then-act (if key not in d: d[key] = ...).
lock = threading.Lock()
with lock: # acquire/release, even on exceptions
counter += 1
threading.Eventfor one-shot "X has happened" signals (set()/wait()).RLockonly if the same thread must re-acquire. Usually that means the design needs rethinking.
Gotchas
- Keep critical sections small, and never do I/O or call unknown callbacks while holding a lock.
x += 1is not atomic in Python, GIL or no GIL.
6 problems
Concurrency
Locks Mutexes, lock ordering, readers and writers.
Lock (mutex) guide
- Thread-safe counter basics py · c++ · java easy
- Last croissants at the bakery py · c++ · java easy
- Print in order with locks py · c++ · java easy
- Alternate foo and bar py · c++ · java easy
- Thread-safe per-key call counter assessment 3 levels Citadel py · c++ · java medium
More practice: 1 problem
- Thread-safe per-user rate limiter 2 levels py · c++ · java medium