-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.cpp
More file actions
49 lines (42 loc) · 1.1 KB
/
Copy pathselect.cpp
File metadata and controls
49 lines (42 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
46
47
48
49
/*
* Select is implemented in a similar fashion as [Select][reflect.Select] found
* in the [reflect][] package.
*
* [reflect.Select]: https://golang.org/pkg/reflect/#Select
* [reflect]: https://golang.org/pkg/reflect/
*/
#include <iostream>
#include <string>
#include <thread>
#include <bongo/bongo.h>
using namespace std::string_literals;
int main() try {
bongo::chan<std::string> c0, c1;
auto t0 = std::thread{[&]() {
c0 << "one"s;
}};
auto t1 = std::thread{[&]() {
c1 << "two"s;
}};
for (long i = 0; i < 2; ++i) {
std::optional<std::string> msg0, msg1;
bongo::select_case cases[] = {
bongo::recv_select_case(c0, msg0),
bongo::recv_select_case(c1, msg1),
};
switch (bongo::select(cases)) {
case 0: // Value corresponds with index 0 in "cases"
std::cout << "received: " << *msg0 << "\n";
break;
case 1: // Value corresponds with index 1 in "cases"
std::cout << "received: " << *msg1 << "\n";
break;
}
}
t0.join();
t1.join();
return 0;
} catch (std::exception const& e) {
std::cerr << e.what() << "\n";
return 1;
}