-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
271 lines (207 loc) · 4.89 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "bits/stdc++.h"
#include <semaphore>
#include <thread>
using namespace std;
// ------------------------------------------ functions prototypes
template <typename T>
void print(T t);
template <typename T>
void debugprint(T t);
template <typename T>
void print(vector<T> v);
template <typename T>
void print(vector<T> v, int start, int end);
// ------------------------------------------ classes
struct MQStatus {
public:
int in{0};
int out{0};
int messages_count{0};
int messages_size{0};
int total_memory_usage{0};
void print() {
std::cout << "in: " << in << '\n';
std::cout << "out: " << out << '\n';
std::cout << "messages_count: " << messages_count << '\n';
std::cout << "messages_size: " << messages_size << '\n';
std::cout << "total_memory_usage: " << total_memory_usage << '\n';
}
};
class AyMQ {
private:
int size{0};
int in{0};
int out{0};
int filled{0};
binary_semaphore q_lock{1};
binary_semaphore q_empty{1}; // 0 -> is empty so wait , 1 -> is not empty go on
std::vector<std::string> messages;
public:
AyMQ(int size) {
messages.resize(size);
}
bool is_full() {
return filled == messages.size();
}
bool is_empty() {
return filled == 0;
}
bool send_msg(std::string new_message) {
q_lock.acquire();
if (is_full()) {
std::cout << "queue is full my bro! message: " << new_message << " ignored" << '\n';
q_lock.release();
return false;
}
q_lock.release();
q_lock.acquire();
messages[in] = new_message;
in = (in + 1) % messages.size();
filled += 1;
q_empty.release();
std::cout << "new message added: " << new_message << '\n';
q_lock.release();
return true;
}
std::string get_msg() {
std::string out_message;
q_lock.acquire();
if (is_empty()) {
q_empty.acquire();
}
out_message = messages[out];
out = (out + 1) % messages.size();
filled -= 1;
std::cout << "front poped: " << out_message << '\n';
q_lock.release();
return out_message;
}
std::string get_msg_nb() {
std::string out_message;
q_lock.acquire();
if (filled == size) {
q_lock.release();
return "queue is empty!";
}
q_lock.release();
q_lock.acquire();
out_message = messages[out];
out = (out + 1) % size;
filled -= 1;
q_lock.release();
return out_message;
}
MQStatus *stats() {
print(messages, out, in);
return new MQStatus{in, out, (int)messages.size(), filled, 0};
}
};
// ------------------------------------------ main
int main()
{
std::cout << "Queue is ready!" << '\n';
// SENARIO1
// AyMQ mq(5);
//
// mq.send_msg("first");
// mq.send_msg("second");
// mq.send_msg("third");
//
// print("");
//
// print(mq.get_msg());
// print(mq.get_msg());
// print(mq.get_msg());
// ENDSENARIO
// SENARIO2
// AyMQ *mq = new AyMQ(5);
//
// std::thread t1(&AyMQ::send_msg, mq, "first");
// std::thread t2(&AyMQ::send_msg, mq, "second");
// std::thread t3(&AyMQ::send_msg, mq, "third");
//
// print("");
//
// std::thread t4(&AyMQ::get_msg, mq);
// std::thread t5(&AyMQ::get_msg, mq);
// std::thread t6(&AyMQ::get_msg, mq);
//
// t1.join();
// t2.join();
// t3.join();
// t4.join();
// t5.join();
// t6.join();
// ENDSENARIO
// SENARIO3
AyMQ *mq = new AyMQ(5);
std::thread t1(&AyMQ::send_msg, mq, "first");
std::thread t2(&AyMQ::send_msg, mq, "second");
std::thread t3(&AyMQ::send_msg, mq, "third");
std::thread t4(&AyMQ::send_msg, mq, "fourth");
std::thread t5(&AyMQ::send_msg, mq, "fifth");
std::thread t6(&AyMQ::send_msg, mq, "sixth");
print("");
std::thread t7(&AyMQ::get_msg, mq);
std::thread t8(&AyMQ::get_msg, mq);
std::thread t9(&AyMQ::get_msg, mq);
// mq->stats();
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
t8.join();
t9.join();
MQStatus *mqs = mq->stats();
mqs->print();
// ENDSENARIO
return 0;
}
// ------------------------------------------ classes
// ------------------------------------------ functions
template <typename T>
void print(T t)
{
std::cout << t << "\n";
}
template <typename T>
void debugprint(T t)
{
std::cout << "debug: " << t << "\n";
}
template <typename T>
void print(vector<T> v)
{
cout << "{";
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << ", ";
}
cout << "}\n";
}
template <typename T>
void print(vector<T> v, int start, int end)
{
if (end > v.size() || start > v.size()) {
std::cout << "end or start is bigger than size of vector?!" << '\n';
return;
}
cout << "{";
if (start <= end) {
for (size_t i = start; i < end; i++)
{
cout << v[i] << ", ";
}
} else {
for (size_t i = start; i < v.size(); i++) {
cout << v[i] << ", ";
}
for (size_t i = 0; i < end; i++) {
cout << v[i] << ", ";
}
}
cout << "}\n";
}