Threads :
- Threads are smaller unit of a process, basically they are a set of functions that allow multiple flows of control, in other terms, you can have multiple tasks running simultaneously within a program, for example you could be counting to 100 and at the same time printing “hello world”.
- Threads share the same memory space within a single process, which makes context switching between threads much faster and resource-efficient compared to processes.
Data race
- When two threads want to access a shared resource at the same time, we get a condition called “data-race”.
Mutexes :
- Mutexes (short for mutual exclusion) are synchronization primitives used in concurrent programming to prevent multiple threads from simultaneously accessing shared resources or critical sections of code that could lead to data corruption or inconsistency.
- Mutexes allow only one thread at a time to execute a critical section of code or access a shared resource. This prevents race conditions where multiple threads modify shared data concurrently, leading to unpredictable behavior.