Synchronisation
Race conditions
- Different program behavior depending on which thread gets there first
- "Unsynchronized access of a shared resource leads to erroneous program behavior"
- hardware, kernel resource, memory
- memory - data race - most common form
💡 · critical region - region of code which needs synchronisation.
· Eliminate races by synchronising thread access to critical regions.
Dead locks
-
Two threads both waiting for each other to finish.
-
One thread blocked on a mutex it already holds.
How to avoid?
- Lock data not code - are multiple locks really required?
- Have a specific order for obtaining locks when multiple data locks are required.
- Release in opposite order obtained.