Created
May 3, 2020 09:11
-
-
Save shiyanhui/df3bc65e9f0486ab9ba4fa1801939490 to your computer and use it in GitHub Desktop.
select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define csp_without_prefix | |
#include <stdio.h> | |
#include <libcsp/csp.h> | |
chan_declare(mm, int, int); | |
chan_define(mm, int, int); | |
proc void select1(chan_t(int) *chn, chan_t(int) *chn2) { | |
while (true) { | |
int num; | |
if (chan_try_pop(chn, &num)) { | |
printf("Received %d from main channel\n", num); | |
return; | |
} | |
if (chan_try_pop(chn2, &num)) { | |
printf("Received %d from main chn2, how odd\n", num); | |
return; | |
} | |
yield(); | |
} | |
} | |
proc void select2(chan_t(int) *chn, chan_t(int) *chn3) { | |
while (true) { | |
if (chan_try_push(chn, 1)) { | |
printf("Written 1 to main channel\n"); | |
return; | |
} | |
if (chan_try_push(chn3, 1)) { | |
printf("Written 1 to ch3, how odd"); | |
return; | |
} | |
yield(); | |
} | |
} | |
int main(void) { | |
chan_t(int) *chn = chan_new(int)(0); | |
chan_t(int) *chn2 = chan_new(int)(0); | |
chan_t(int) *chn3 = chan_new(int)(0); | |
async(select1(chn, chn2); select2(chn, chn3)); | |
hangup(timer_second); | |
chan_destroy(chn); | |
chan_destroy(chn2); | |
chan_destroy(chn3); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment