-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex.cpp
More file actions
41 lines (29 loc) · 907 Bytes
/
Copy pathmutex.cpp
File metadata and controls
41 lines (29 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// TOPIC: Mutex in C++ Threading |Why Use Mutex | What is Race Condition And How to solve it ? | What is critical Section
// Mutex : 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 section is called critical section/region.
// MUTEX :
// 0.Mutex is used to avoid race condition.
// 1. We us 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);
std:: thread t3(addMoney);
t1.join();
t2.join();
t3.join();
cout<<myAmount<<endl;
return 0;
}