-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_waitset_pub_sub.cpp
157 lines (134 loc) · 3.13 KB
/
test_waitset_pub_sub.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
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include "waitset/waitset.hpp"
//just dummies, without queues and very trivial data sharing but the basic principle should apply
class Subscriber
{
public:
//could also register the token in a delayed fashion
Subscriber(int id) : m_id(id)
{
}
std::optional<WaitToken> registerWaitSet(WaitSet &waitSet)
{
auto condition = [&]() { return this->hasData(); };
auto callback = [&]() {
auto maybeData = take();
std::cout << "subscriber id " << m_id << " received ";
if (maybeData.has_value())
{
std::cout << *maybeData << std::endl;
}
else
{
std::cout << " nothing" << std::endl;
}
};
m_token = waitSet.add(condition, callback);
return m_token;
}
std::optional<int> take()
{
if (hasData())
{
//yes there is a race with deliver, but this is not a real publisher/subscriber just a waitset experiment:-)
m_hasData = false;
return m_data;
}
return std::nullopt;
}
void deliver(int data)
{
m_hasData = true;
m_data = data;
if (m_token.has_value())
(*m_token).notify();
}
bool hasData()
{
return m_hasData;
}
private:
int m_id;
bool m_hasData{false};
int m_data;
std::optional<WaitToken> m_token;
};
class Publisher
{
public:
Publisher()
{
}
void registerSubscriber(Subscriber &sub)
{
m_subscribers.push_back(&sub);
}
void publish(int data)
{
for (auto subscriber : m_subscribers)
{
subscriber->deliver(data);
}
}
private:
std::vector<Subscriber *> m_subscribers;
};
std::atomic<bool> run{true};
WaitSet waitSet(10);
Publisher p1;
Publisher p2;
Subscriber s1(1);
Subscriber s2(2);
Subscriber s3(3);
bool guardCondition()
{
return run == false;
}
auto guard = waitSet.add(guardCondition, []() { std::cout << "\nguard callback\n"; }).value();
void publish()
{
int i = 0;
while (run)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
p1.publish(++i);
if (i % 2 == 0)
{
p2.publish(2 * i); //publishes slower
}
}
guard.notify(); //free the potentially waiting waiter thread
}
void waitForData()
{
while (run)
{
auto ids = waitSet.wait();
//auto ids = waitSet.wait(myFilter);
std::cout << "woke up with ids: ";
for (auto id : ids)
{
std::cout << id << " ";
}
std::cout << std::endl;
}
}
int main(int argc, char **argv)
{
s1.registerWaitSet(waitSet);
s2.registerWaitSet(waitSet);
s3.registerWaitSet(waitSet);
p1.registerSubscriber(s1);
p1.registerSubscriber(s2);
p2.registerSubscriber(s3);
std::thread waiter(waitForData);
std::thread publisher(publish);
std::this_thread::sleep_for(std::chrono::seconds(11));
run = false;
waiter.join();
publisher.join();
return 0;
}