Race conditions
It’s very easy to introduce subtle programing errors into concurrent programs
Unidentified or incorrectly-protected critical sections leading to race conditions
Why subtle?
- In a sequential program, given identical input, things happen deterministically (i.e. in the same order)
- This is not the case in a concurrent program (sometimes work)
- This has led to the design of special concurrent programming languages that try to aid correctness (e.g., Eiffel, CSP)
Deadlock Scenario
Deadlock
Deadlock in Threads
Very typical with incorrect lock ordering
Dealing with Deadlock
Deadlocks and Locking Order
Livelock Intuition
Two cars coming from opposite directions have to cross a single-lane bridge.
Cars get on the bridge, see each other. Each back offs so the other can pass.
Ad infinitum
Livelock
Starvation
Some process is unable to obtain access to shared resources
Dining Philoshopher
Solution? Each picks up an available fork, then waits for the other fork to become available
Deadlock!
Dining Philoshopher
Each picks up an available fork, then checks for the other fork.
If unavailable, put downs the held fork. Retries. Solution?
Livelock!
Dining Philoshopher
Each picks lower-numbered fork first and then highernumbered fork
Prevents deadlock
But not starvation: C eats, then thinks, but
before P can pick up 2, picks it up again…ad
infinitum
Need fair scheduling to guarantee that P
eventually eats
Paranoid Programming
If you are dealing with concurrent design patterns,
Approach the problem methodically