-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutex.cpp
More file actions
44 lines (32 loc) · 940 Bytes
/
Mutex.cpp
File metadata and controls
44 lines (32 loc) · 940 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
42
43
44
/* 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 happens 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 use lock() , unlock() on mutex to avoid race condition. */
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
std::mutex m;
int myMoney = 0;
void AddMoney()
{
m.lock();
myMoney++;
m.unlock();
}
int main()
{
std::thread t1(AddMoney);
std::thread t2(AddMoney);
if(t1.joinable())
t1.join();
if(t2.joinable())
t2.join();
cout<<myMoney<<endl;
return 0;
}
// compile: g++ -std=c++11 Mutex.cpp -pthread