chapter 32 - common concurrency problems Flashcards
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 can hold-and-wait work
Acquire all locks at once using an extra lock that serializes locks - L1, så L2 så L3 osv
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 -> solved by adding random delays or backoff strategies
How can mutual exclusion be avoided?
Use lock-free structures with atomic operations like Compare-And-Swap
Why is avoiding mutual exclusion effective?
It removes the need for locks, and thus the potential for deadlocks
complex dependencies
easy to to make cross-dependencies that makes a cycle where each parts wait for eachother
encapsulation conflicts with locking
Object-oriented encourages hiding implementation details, but lock order often depends on those details
why does deadlocks occur
- complex dependencies
- encapsulation conflicts with locking