-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
154 lines (114 loc) · 4.15 KB
/
main.cpp
File metadata and controls
154 lines (114 loc) · 4.15 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
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
#include <iostream>
#include <map>
#include <thread>
#include <chrono>
#include <mutex>
#include <algorithm>
#include <queue>
#include <vector>
#include <condition_variable>
std::mutex print_mutex;
void secure_print(std::string msg)
{
std::lock_guard<std::mutex> print_lock(print_mutex);
std::cout << "[ "<< std::this_thread::get_id() << " ]"<< msg << std::endl;
}
template<typename T>
class MQueue
{
public:
template<typename cont>
explicit MQueue(const cont& c)
{
for(auto e: c)
_collection.push(e);
}
T dequeue()
{
std::lock_guard<std::mutex> lock{_mutex};
auto c = _collection.front();
_collection.pop();
return c;
}
bool empty()
{
return _collection.empty();
}
private:
std::queue<T> _collection;
std::mutex _mutex;
};
int main()
{
std::vector<std::thread> pool;
int x{7}, y{11}, z{3};
std::mutex x_mutex, y_mutex, z_mutex, g_mutex;
std::condition_variable pump_condition;
std::vector<int> cars{2, 8, 4, 3, 2};
std::vector<int> wait_times(cars.size());
MQueue<int> q{cars};
std::atomic<int> wait_times_counter{0};
for(auto i(0); i < 3; ++i)
{
auto begin = std::chrono::steady_clock::now();
pool.emplace_back([&]() {
while(!q.empty()) {
auto ltr_reqd = q.dequeue();
std::cout << ltr_reqd<< std::endl;
auto idx = wait_times_counter++;
while (true) {
if (x_mutex.try_lock()) {
if (x >= ltr_reqd) {
auto now = std::chrono::steady_clock::now();
wait_times[idx] = std::chrono::duration_cast<std::chrono::seconds>(now - begin).count();
secure_print(std::to_string(ltr_reqd) + " serviced by x");
std::this_thread::sleep_for(std::chrono::seconds(ltr_reqd));
x -= ltr_reqd;
x_mutex.unlock();
pump_condition.notify_all();
break;
}
x_mutex.unlock();
pump_condition.notify_all();
}
if (y_mutex.try_lock()) {
if (y >= ltr_reqd) {
auto now = std::chrono::steady_clock::now();
wait_times[idx] = std::chrono::duration_cast<std::chrono::seconds>(now - begin).count();
secure_print(std::to_string(ltr_reqd) + " serviced by y");
std::this_thread::sleep_for(std::chrono::seconds(ltr_reqd));
y -= ltr_reqd;
y_mutex.unlock();
pump_condition.notify_all();
break;
}
y_mutex.unlock();
pump_condition.notify_all();
}
if (z_mutex.try_lock()) {
if (z >= ltr_reqd) {
auto now = std::chrono::steady_clock::now();
wait_times[idx] = std::chrono::duration_cast<std::chrono::seconds>(now - begin).count();
secure_print(std::to_string(ltr_reqd) + " serviced by z");
std::this_thread::sleep_for(std::chrono::seconds(ltr_reqd));
z -= ltr_reqd;
z_mutex.unlock();
pump_condition.notify_all();
break;
}
z_mutex.unlock();
pump_condition.notify_all();
}
std::unique_lock<std::mutex> g_lock(g_mutex);
secure_print("wait for pumps to be free");
pump_condition.wait(g_lock);
}
}
});
}
for(auto& t : pool)
t.join();
for(auto& wtime : wait_times)
std::cout << wtime << std::endl;
return 0;
}