Demonstration of Race Condition and its Solution using Mutex
Demonstration of Race Condition and Its Solution Using Mutex
1. Aim
To demonstrate a race condition in a multithreaded program and to eliminate it using a mutex for proper synchronization.
2. Objectives
-
To understand race conditions in multithreading
-
To observe incorrect results due to unsynchronized access
-
To apply mutex locks to ensure mutual exclusion
-
To compare behavior with and without synchronization
3. Theory
Race Condition
A race condition occurs when:
-
Two or more threads access shared data
-
At least one thread modifies it
-
Execution order is unpredictable
This results in incorrect or inconsistent output.
Mutex (Mutual Exclusion)
A mutex ensures:
-
Only one thread can access a critical section at a time
-
Prevents simultaneous updates to shared data
4. Problem Description
Create two threads:
-
Each thread adds two numbers
-
Both threads update a shared global variable
-
Observe incorrect output (race condition)
-
Fix the issue using a mutex
5. Algorithm
Part A: Without Synchronization (Race Condition)
-
Declare a shared variable
total_sum -
Create two threads
-
Each thread adds its numbers to
total_sum -
Main thread prints the result
Part B: With Synchronization (Mutex)
-
Initialize a mutex
-
Lock the mutex before updating
total_sum -
Unlock mutex after update
-
Print the correct result
6. Program – Part A: Race Condition (Without Mutex)
Observation (Without Mutex)
binuvp@debian-workstation:~$ ./a.out
Final Sum (Without Mutex) = 30
binuvp@debian-workstation:~$ ./a.out
Final Sum (Without Mutex) = 70
binuvp@debian-workstation:~$ ./a.out
Final Sum (Without Mutex) = 70
➡ Output varies due to race condition
7. Program – Part B: Race Condition Solved Using Mutex
8. Compilation & Execution
9. Sample Output
binuvp@debian-workstation:~$ ./a.out
Final Sum (With Mutex) = 70
10. Observations
| Case | Result |
|---|---|
| Without mutex | Incorrect / inconsistent |
| With mutex | Correct and consistent |
Comments
Post a Comment