Skip to content

Instantly share code, notes, and snippets.

@shiyanhui
Created May 3, 2020 09:11
Show Gist options
  • Save shiyanhui/df3bc65e9f0486ab9ba4fa1801939490 to your computer and use it in GitHub Desktop.
Save shiyanhui/df3bc65e9f0486ab9ba4fa1801939490 to your computer and use it in GitHub Desktop.
select
#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