-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathselect.c
56 lines (47 loc) · 1.41 KB
/
select.c
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
#include <stdlib.h>
#include <unistd.h>
#include "../neco.h"
void coroutine1(int argc, void *argv[]) {
neco_chan *c1 = argv[0];
neco_sleep(NECO_SECOND / 2);
char *msg = "one";
neco_chan_send(c1, &msg);
neco_chan_release(c1);
}
void coroutine2(int argc, void *argv[]) {
neco_chan *c2 = argv[0];
neco_sleep(NECO_SECOND / 2);
char *msg = "two";
neco_chan_send(c2, &msg);
neco_chan_release(c2);
}
int neco_main(int argc, char *argv[]) {
// For our example we’ll select across two channels.
neco_chan *c1;
neco_chan *c2;
neco_chan_make(&c1, sizeof(char*), 0);
neco_chan_make(&c2, sizeof(char*), 0);
// Each channel will receive a value after some amount of time, to
// simulate e.g. blocking RPC operations executing in concurrent coroutines.
neco_chan_retain(c1);
neco_start(coroutine1, 1, c1);
neco_chan_retain(c2);
neco_start(coroutine2, 1, c2);
// We’ll use neco_chan_select() to await both of these values
// simultaneously, printing each one as it arrives.
for (int i = 0; i < 2; i++) {
char *msg = NULL;
switch (neco_chan_select(2, c1, c2)) {
case 0:
neco_chan_case(c1, &msg);
break;
case 1:
neco_chan_case(c2, &msg);
break;
}
printf("received %s\n", msg);
}
neco_chan_release(c1);
neco_chan_release(c2);
return 0;
}