-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition_variable.cpp
More file actions
64 lines (45 loc) · 1.38 KB
/
Copy pathcondition_variable.cpp
File metadata and controls
64 lines (45 loc) · 1.38 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
/*
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 <bits/stdc++.h>
using namespace std;
std::condition_variable cv;
std::mutex mtx;
int balance = 0;
// Correct function to check balance
bool checkBalance() {
return balance > 0;
}
void deposit(int amt) {
std::unique_lock<std::mutex> lock(mtx);
balance += amt;
cout << "Amount added and Current Balance is: " << balance << endl;
cv.notify_one(); // Notify waiting thread
}
void withdraw(int amt) {
std::unique_lock<std::mutex> lock(mtx);
// Wait until condition get verified
cv.wait(lock, checkBalance);// can use lambda fxn here bool checkBalance() { return balance > 0; }
if (balance >= amt) {
balance -= amt;
cout << "Amount deducted: " << amt << " | Current Balance: " << balance << endl;
} else {
cout << "Balance is less than the withdrawal amount!" << endl;
}
}
int main() {
std::thread t2(withdraw, 50);
std::thread t1(deposit, 100);
t1.join();
t2.join();
return 0;
}