Skip to content

Instantly share code, notes, and snippets.

@yefremov
Created July 3, 2017 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yefremov/83351575e76f6bb60506050369a58528 to your computer and use it in GitHub Desktop.
Save yefremov/83351575e76f6bb60506050369a58528 to your computer and use it in GitHub Desktop.
Coroutines in C
//
// coro.c
//
// Based on a a Duff's device
// https://pusher.com/sessions/meetup/the-realtime-guild/realtime-stream-processing-with-coroutines
//
/*
* Coroutine.
*/
int
coro() {
static int i, s = 0;
switch (s) {
case 0:
for (i = 0; ; i++) {
if (!s) s = 1;
return i;
case 1:;
}
}
}
/*
* Run program.
*/
int
main() {
printf("%d\n", coro()); // 0
printf("%d\n", coro()); // 1
printf("%d\n", coro()); // 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment