A mutex (mutual exclusion) is a synchronization primitive that ensures only one thread at a time can access a shared resource or critical section. A thread locks the mutex before accessing the resource and unlocks it after, preventing race conditions.
Happens when multiple thread access shared resource concurrently and result depends on the order of execution.
- protect the shared data with locks or use atomics for simpler types
Happens when two or more thread wait forever for each others lock.
- lock and unlock resources in order, best to use modern lock guards over mutexes or choose lock free designs
Race condition is logic/behaviour related issue while deadlock is resource acquisition related
A condition variable lets a thread sleep efficiently until a condition becomes true, instead of busy-waiting.