Deadlocks are a challenge in parallel computing. Essentially the problem boils down to a circular dependency between threads. One thread is waiting for another thread’s mutex to unlock, and the other thread is waiting for the first thread’s mutex to unlock. Thus it’ll never unlock since they depend on each other.
Deadlocks have a few conditions:
- Mutual exclusion
- Hold and wait (you have a lock and try to acquire another)
- No pre-emption (we can’t take locks away)
- Circular wait (waiting for a lock held by another process)
How do we prevent deadlocks?
- Ensure order of locks (i.e., we acquire locks one after the other, then unlock in the opposite order).
- Could use
trylock