chapter 32 - common concurrency problems Flashcards
(10 cards)
what is a deadlock
A situation where two or more threads wait on each other to release resources, and none can proceed
Give an example of a deadlock
Thread 1 holds Lock A, waits for Lock B; Thread 2 holds Lock B, waits for Lock A
What are the 4 Coffman Conditions for deadlock? - deadlock only occurs if all four of these are hold
-Mutual Exclusion
-Hold and Wait
-No Preemption
-Circular Wait
how does circular wait work
use a global ordering for acquiring locks - always acquire locks in a specific order
How does hold-and-wait work
A process is holding one resource and waiting to acquire another
Thread A holds Lock1, and wants Lock2 (which is held by another thread)
How does trylock help prevent deadlock?
it lets a thread skip or back off if it can’t acquire a lock, avoiding wait cycles
What problem can trylocks cause and how to fix it?
Livelock - acquire a lock without blocking. If the lock is taken, it fails instead of waiting
solved by adding random delays or backoff strategies
Why is avoiding mutual exclusion effective?
It removes the need for locks, and thus the potential for deadlocks
complex dependencies
Each thread depends on multiple locks
circular dependency and deadlock risk
encapsulation conflicts with locking
Hiding implementation details, but lock order often depends on those details