Created
May 24, 2019 22:31
-
-
Save caloni/4cebe69ab95b1ff88dbb06789342ffe1 to your computer and use it in GitHub Desktop.
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 "picoro/picoro.h" | |
#include <stdio.h> | |
#include <gmodule.h> | |
void* print_number(void* arg) | |
{ | |
int* number = (int*)arg; | |
int counter = *number; | |
while (counter--) | |
{ | |
g_usleep(1000); | |
yield(arg); | |
} | |
printf("%d\n", *number); | |
return NULL; | |
} | |
int main() | |
{ | |
int counter = 0; | |
int i; | |
int resume_again = 1; | |
int v[] = { 8, 42, 38, 111, 2, 39, 1 }; | |
coro coroutines[sizeof(v) / sizeof(int)]; | |
for (i = 0; i < sizeof(v) / sizeof(int); ++i) | |
coroutines[i] = coroutine(print_number); | |
while (resume_again) | |
{ | |
resume_again = 0; | |
for (i = 0; i < sizeof(v) / sizeof(int); ++i) | |
{ | |
if (resumable(coroutines[i])) | |
{ | |
resume(coroutines[i], &v[i]); | |
resume_again = 1; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment