Created
August 28, 2013 18:33
-
-
Save laindir/6369550 to your computer and use it in GitHub Desktop.
Example of a coroutine/generator in C
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
#include <stdio.h> | |
#include "coroutine.h" | |
typedef struct | |
{ | |
int max_x; | |
int max_y; | |
int state; | |
int x; | |
int y; | |
} iter; | |
int | |
iterate(iter *i) | |
{ | |
start(i->state); | |
for(i->x = 0; i->x < i->max_x; i->x++) | |
{ | |
for(i->y = 0; i->y < i->max_y; i->y++) | |
{ | |
yield(i->state, 1); | |
} | |
} | |
finish; | |
return 0; | |
} | |
int | |
main(void) | |
{ | |
iter i = {2, 2}; | |
while(iterate(&i)) | |
{ | |
printf("%d %d\n", i.x, i.y); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
coroutine.h defined in https://gist.github.com/laindir/6369535