Skip to content

Instantly share code, notes, and snippets.

@laindir
Created August 28, 2013 18:33
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 laindir/6369550 to your computer and use it in GitHub Desktop.
Save laindir/6369550 to your computer and use it in GitHub Desktop.
Example of a coroutine/generator in C
#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;
}
@laindir
Copy link
Author

laindir commented Aug 28, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment