-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionVariable.cpp
More file actions
64 lines (54 loc) · 1.43 KB
/
ConditionVariable.cpp
File metadata and controls
64 lines (54 loc) · 1.43 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* TOPIC: Condition Variable In C++ Threading
NOTES:
1. Condition variables allow us to synchronize threads via notifications.
a. notify_one();
b. notify_all();
2. You need mutex to use condition variable
3. Condition variable is used to synchronize two or more threads.
4. Best use case of condition variable is Producer/Consumer problem.
5. Condition variables can be used for two purposes:
a. Notify other threads
b. Wait for some condition */
#include<iostream>
#include<thread>
#include<condition_variable>
#include<mutex>
using namespace std;
long balance = 0;
std::condition_variable cv;
std::mutex m;
void AddMoney(const int & money)
{
std::lock_guard<mutex> lg(m);
balance+=money;
cout<<"Amount added current balance " <<balance<<endl;
cv.notify_one();
}
void WithdrawMoney(const int & money)
{
std::unique_lock<mutex> ul(m);
cv.wait(ul,[]{return (balance!=0)?true:false;});
if(balance>=money)
{
balance-=money;
cout<<"Ammount deducted "<<money<<endl;
}
else
{
cout<<"Ammount cant be deducted balance is less than "<<money<<endl;
}
cout<<"Current balance is "<<balance<<endl;
}
int main()
{
std::thread t1(WithdrawMoney, 500);
std::thread t2(AddMoney, 500);
t1.join();
t2.join();
return 0;
}
// Compile: g++ ConditionVariable.cpp -std=c++11 -pthread
/* output:
Amount added current balance 500
Ammount deducted 500
Current balance is 0 */