• May 31, 2025

Mutex in C++ threading | Why use mutex | What is race condition and how to solve it? | What is critical section

Mutex Stands For Mutual Exclusion

RACE CONDITION:
0. Race condition is a situation where two or more threads/process happend to change a common data at the same time.
1. If there is a race condition then we have to protect it and the protected setion is  called critical section/region.

MUTEX:
0. Mutex is used to avoid race condition.
1. We use lock() , unlock() on mutex to avoid race condition.

#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

int myAmount = 0;
std::mutex m;

void addMoney() {
m.lock();
++myAmount;
m.unlock();
}

int main() {
std::thread t1(addMoney);
std::thread t2(addMoney);

t1.join();
t2.join();

cout << myAmount << endl;
return 0;
}

Post a Comment

1 Comments

  1. I executed the above program several times without mutex but I haven't see any problem "Output myAmount =2" ,is it possible to see myAmount =1 due to race condition.

    ReplyDelete