-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmutex.cpp
179 lines (155 loc) · 4.35 KB
/
mutex.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <fstream>
#include <iostream>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
/*
Timed mutex
try_lock_for and try_lock_until
mutex.try_lock()
Unique lock
*/
namespace noMutex {
void function2() {
unsigned int sleepTime = 1; // milliseconds
for (std::size_t i = 0; i < 100; i++) {
std::cout << "Message from function1: " << i << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
void function1() {
unsigned int sleepTime = 1; // milliseconds
for (int i = 0; i > -100; i--) {
std::cout << "Message from function1: " << i << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
} // namespace noMutex
namespace mutexWithNoDeadLockProtection {
std::mutex mu;
void sharedPrinter(std::string s, int id) {
mu.lock();
std::cout << "Message from function" << id << ": " << s << std::endl;
mu.unlock();
}
void function2() {
unsigned int sleepTime = 1; // milliseconds
for (std::size_t i = 0; i < 100; i++) {
sharedPrinter(std::to_string(i), 2);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
void function1() {
unsigned int sleepTime = 1; // milliseconds
for (int i = 0; i > -100; i--) {
sharedPrinter(std::to_string(i), 1);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
} // namespace mutexWithNoDeadLockProtection
namespace mutexWithLockGuard {
std::mutex mu;
void sharedPrinter(std::string s, int id) {
// here when gaurd goes out of scope, mutex get unlocked
std::lock_guard<std::mutex> gaurd(mu);
std::cout << "Message from function" << id << ": " << s << std::endl;
}
void function2() {
unsigned int sleepTime = 1; // milliseconds
for (std::size_t i = 0; i < 100; i++) {
sharedPrinter(std::to_string(i), 2);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
void function1() {
unsigned int sleepTime = 1; // milliseconds
for (int i = 0; i > -100; i--) {
sharedPrinter(std::to_string(i), 1);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
} // namespace mutexWithLockGuard
namespace mutexWithStreamProtection {
class LogFile {
std::mutex mutex;
// RAII (Resource acquisition is initialization)
public:
std::ofstream fileObj;
LogFile(std::string logFile = "Log.txt") {
fileObj.open(logFile, std::ofstream::out);
}
void sharedPrinter(std::string s, int id) {
std::lock_guard<std::mutex> guard(mutex);
fileObj << "Message from function" << id << ": " << s << std::endl;
}
~LogFile() { fileObj.close(); }
};
void function1Log(LogFile &log) {
unsigned int sleepTime = 10; // milliseconds
for (int i = 0; i > -100; i--) {
log.sharedPrinter(std::to_string(i), 1);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
void function2Log(LogFile &log) {
unsigned int sleepTime = 10; // milliseconds
for (int i = 0; i < 100; i++) {
log.sharedPrinter(std::to_string(i), 2);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
}
}
} // namespace mutexWithStreamProtection
class walletMutex {
public:
int money;
std::mutex mutex;
walletMutex() : money(0) {}
void increaseMoney(int amount) {
mutex.lock();
money = money + amount;
mutex.unlock();
}
};
int fixingRaceConditions() {
walletMutex walletObject;
std::vector<std::thread> threads;
for (int i = 0; i < 5; ++i) {
threads.push_back(
std::thread(&walletMutex::increaseMoney, &walletObject, 10));
}
for (int i = 0; i < threads.size(); i++) {
threads.at(i).join();
}
return walletObject.money;
}
void fixingRaceConditionsExample() {
int val = 0;
for (int k = 0; k < 10000; k++) {
if ((val = fixingRaceConditions()) != 50) {
std::cout << "Error at count = " << k << " Money in Wallet = " << val
<< std::endl;
}
}
}
int main() {
{
std::thread t1(noMutex::function1);
std::thread t2(noMutex::function2);
t1.join();
t2.join();
}
{
std::thread t1(mutexWithNoDeadLockProtection::function1);
std::thread t2(mutexWithNoDeadLockProtection::function2);
t1.join();
t2.join();
}
{
mutexWithStreamProtection::LogFile log;
std::thread t1(mutexWithStreamProtection::function1Log, std::ref(log));
std::thread t2(mutexWithStreamProtection::function2Log, std::ref(log));
t1.join();
t2.join();
}
}