-
Notifications
You must be signed in to change notification settings - Fork 11
/
channel_loop.cc
65 lines (57 loc) · 1.72 KB
/
channel_loop.cc
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
#include <unistd.h>
#include <chrono>
#include <iostream>
#include "boson/boson.h"
#include "boson/channel.h"
#include "boson/logger.h"
using namespace std::literals;
static constexpr int nb_iter = 2;
static constexpr int channel_size = 5;
int main(int argc, char* argv[]) {
// Set global logger
boson::debug::logger_instance(&std::cout);
// Execute a routine communication through channels
// Launch an engine with 3 threads
boson::run(3, []() {
using namespace boson;
channel<int, channel_size> a2b;
channel<int, channel_size> b2a;
channel<int, 1> b2c;
channel<int, 1> c2b;
// Start a producer
start([](auto in, auto out) -> void {
int ack_result = 0;
for (int i = 0; i < nb_iter; ++i) {
// Send async
for (int j = 0; j < channel_size; ++j) {
out << (i * channel_size + j);
boson::debug::log("A: sent {}.", i * channel_size + j);
}
// get ack
for (int j = 0; j < channel_size; ++j) {
in >> ack_result;
if (ack_result == i * channel_size + j) boson::debug::log("A: ack succeeded.");
}
}
}, b2a, a2b);
// Start a router
start([](auto source_in, auto source_out, auto dest_in, auto dest_out) -> void{
int result = 0;
while (result < nb_iter * channel_size - 1) {
source_in >> result;
dest_out << result;
dest_in >> result;
source_out << result;
}
},a2b, b2a, c2b, b2c);
// Start a consumer
start([](auto in, auto out) -> void {
int result = 0;
while (result < nb_iter * channel_size - 1) {
in >> result;
boson::debug::log("C received: {}", result);
out << result;
}
}, b2c, c2b);
});
}