Atomicity Violation: Lack of mutex inside critical section
- Serializability: Outcome of concurrent operations == sequential of the same operations
- Can be fixed by adding a lock
Order Violation: Incorrect ordering of operations
Deadlock: Circular waiting
- Thread A is waiting on resource R1 (held by B) while B is waiting for R2 (held by A)
Deadlock Conditions
- Mutex: Only 1 process can use a resource
- Hold & Wait: Hold resources will waiting assignment for others
- No preemption: No resources can be forcibly removed from a process
- Circular Wait: Each process holds a resource that the next is waiting for
Deadlock Prevention
Deadlock Prevention: Break either one of the 4 conditions
Break Mutex: Use atomic hardware instructions that doesn’t need mutex
- Compare-And-Swap (CAS) Instruction: If *addr == expected, set it to the new value
- Con: Complex operations are harder to convert to atomic hardware instructions
Break Hold & Wait
- Acquire all resources at once from start
- Con: Reduce performance (holding a lock that you don’t use)
- Trylock: Try to acquire lock A, if failed, release lock B and try again
Break No Preemption: Forcefully take a lock from a process
Break Circular Wait: Assign a linear order to resources ⭐️
(e.g. must lock R1 before R2)
- Hard to come up with total order when multiple resource types are present
Deadlock Detection: Use graph algorithms to detect deadlocks