-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathping.cpp
45 lines (39 loc) · 1.05 KB
/
ping.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
#include <experimental/channel>
#include <experimental/strand>
#include <experimental/timer>
#include <experimental/yield>
#include <iostream>
#include <memory>
#include <string>
using std::experimental::channel;
using std::experimental::dispatch;
using std::experimental::dispatch_after;
using std::experimental::strand;
using std::experimental::system_executor;
using std::experimental::wrap;
using std::experimental::yield_context;
void pinger(std::shared_ptr<channel<std::string>> c, yield_context yield)
{
for (;;)
{
c->put("ping", yield);
}
}
void printer(std::shared_ptr<channel<std::string>> c, yield_context yield)
{
for (;;)
{
std::string msg = c->get(yield);
std::cout << msg << std::endl;
dispatch_after(std::chrono::seconds(1), yield);
}
}
int main()
{
auto c = std::make_shared<channel<std::string>>();
strand<system_executor> s;
dispatch(wrap(s, [c](yield_context yield){ pinger(c, yield); }));
dispatch(wrap(s, [c](yield_context yield){ printer(c, yield); }));
std::string input;
std::getline(std::cin, input);
}