-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26-thread.cc
72 lines (59 loc) · 1.73 KB
/
26-thread.cc
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
65
66
67
68
69
70
71
72
//
// Program
// Mutex management (std::unique_lock). Two threads try to modify the
// object. After getting hold of mutex before doing actual modification
// thread will sleep for a while forcing other thread to wait until it
// becomes free.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -pthread -o 26-thread 26-thread.cc
//
// Execution
// ./26-thread
//
#include <iostream>
#include <thread>
#include <mutex>
using namespace std::chrono_literals;
//
// Global data shared b/w threads
//
struct Account {
Account(std::uint32_t id, std::uint32_t balance): _id(id), _balance(balance) {}
std::string to_string() {
return std::string("Account # ") + std::to_string(_id) + std::string (" is having balance ") + std::to_string(_balance);
}
std::uint32_t _id;
std::uint32_t _balance;
std::mutex _mutex;
};
void add_balance(std::uint32_t amount, Account &to) {
// Create a lock without locking mutex
std::unique_lock<std::mutex> lock(to._mutex, std::defer_lock);
volatile bool done = false;
while (done == false) {
if (lock.try_lock()) {
std::cout << std::this_thread::get_id() << " ()" << '\n';
std::this_thread::sleep_for(2s);
to._balance += amount;
done = true;
}
else {
std::cout << std::this_thread::get_id() << " ++" << '\n';
}
}
}
//
// Entry function
//
int main() {
std::cout << "--- Unique lock ---" << '\n';
Account p1(10, 500);
std::cout << p1.to_string() << '\n';
std::thread t1(add_balance, 100, std::ref(p1));
std::thread t2(add_balance, 300, std::ref(p1));
t1.join(); // Wait for the thread until it completes its execution
t2.join(); // Wait for the thread until it completes its execution
std::cout << p1.to_string() << '\n';
return 0;
}