32. Common concurrency problems Flashcards
1
Q
What are two most common concurrency problems?
A
Atomicity violation and order violation.
1
Q
What four conditions must be met for a deadlock?
A
- Mutual exclusion: Threads claim exclusive control of resources that they require (e.g., a thread grabs a lock).
- Hold-and-wait: Threads hold resources allocated to them (e.g.,locks that they have already acquired) while waiting for additional resources (e.g., locks that they wish to acquire).
- No preemption: Resources (e.g., locks) cannot be forcibly removed from threads that are holding them.
- Circular wait:There exists a circular chain of threads such that each thread holds one or more resources (e.g., locks) that are being requested by the next thread in the chain.
2
Q
What can we do to prevent curicular wait?
A
Total or partial ordering
3
Q
What can we do to avoid hold-and-wait?
A
Acquire all locks at once, atomically (through additional lock).
4
Q
How can we avoid no preemtpion?
A
By using more advanced interfaces that don’t get stuck waiting for lock such as pthread_mutex_trylock() .
5
Q
How can we avoid mutual exclusion?
A
By not using mutual exclusion. This is accomplished by using wait-free and lock-free data structures, such as those that use atomic machine instructions (test-and-set, compare-and-swap and so on).