- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.cpp
More file actions
Latest commit
45 lines (37 loc) · 1.1 KB
/
Copy pathmain_test.cpp
File metadata and controls
45 lines (37 loc) · 1.1 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
#include"messagebroker.hpp"
#include<iostream>
#include<thread>
#include<vector>
usingnamespacestd;
intmain() {
MessageBroker broker;
broker.subscribe(1, 1001);
broker.subscribe(1, 1002);
auto producer = [&broker]() {
for (int i = 0; i < 10; ++i) {
string data = "msg_" + to_string(i);
cout << "Publishing: " << data << endl;
broker.publish(1, 1, data);
this_thread::sleep_for(chrono::milliseconds(100));
}
};
auto consumer = [&broker](int64_t id) {
for (int i = 0; i < 10; ++i) {
Message msg;
if (broker.consume(1, id, msg)) {
cout << "Consumer " << id << " got: " << msg.payload
<< " at " << msg.timestamp << endl;
} else {
cout << "Consumer " << id << " has no message" << endl;
}
this_thread::sleep_for(chrono::milliseconds(150));
}
};
thread t1(producer);
thread t2(consumer, 1001);
thread t3(consumer, 1002);
t1.join();
t2.join();
t3.join();
return0;
}