-
Notifications
You must be signed in to change notification settings - Fork 2
/
non-blocking-channel-operations.cpp
91 lines (85 loc) · 1.93 KB
/
non-blocking-channel-operations.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
// https://gobyexample.com/non-blocking-channel-operations
//
// package main
//
// import "fmt"
//
// func main() {
// messages := make(chan string)
// signals := make(chan bool)
//
// select {
// case msg := <-messages:
// fmt.Println("received message", msg)
// default:
// fmt.Println("no message received")
// }
//
// msg := "hi"
// select {
// case messages <- msg:
// fmt.Println("sent message", msg)
// default:
// fmt.Println("no message sent")
// }
//
// select {
// case msg := <-messages:
// fmt.Println("received message", msg)
// case sig := <-signals:
// fmt.Println("received signal", sig)
// default:
// fmt.Println("no activity")
// }
// }
#include <eo/fmt.h>
using namespace eo;
func<> eo_main() {
auto messages = make_chan<std::string>();
auto signals = make_chan<bool>();
{
auto select = Select{*messages, CaseDefault{}};
switch (co_await select.index()) {
case 0: {
auto msg = co_await select.process<0>();
fmt::println("received message {}", msg);
break;
}
default:
fmt::println("no message received");
break;
}
}
{
auto msg = "hi";
auto select = Select{(messages << msg), CaseDefault{}};
switch (co_await select.index()) {
case 0: {
co_await select.process<0>();
fmt::println("sent message {}", msg);
break;
}
default:
fmt::println("no message sent");
break;
}
}
{
auto select = Select{*messages, *signals, CaseDefault{}};
switch (co_await select.index()) {
case 0: {
auto msg = co_await select.process<0>();
fmt::println("received message {}", msg);
break;
}
case 1: {
auto signal = co_await select.process<1>();
fmt::println("received signal {}", signal);
break;
}
default:
fmt::println("no activity");
break;
}
}
}