Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Prototype cooperative multitasking.

22 views
Skip to first unread message

luser- -droog

unread,
Jan 27, 2012, 3:03:30 AM1/27/12
to
It doesn't /do/ much; but the approach seems sound.


#include <stdio.h>
#include <setjmp.h>

jmp_buf dispatch;
int ntasks;
void (*task[10])(void);
int quit;

void yield(void) {
longjmp(dispatch, 1);
}

void loop() {
static int i = 0;
if(setjmp(dispatch))
i = (i+1) % ntasks;
while(!quit)
task[i]();
}

int acc = 0;

void a(void) {
if (acc > 10) quit = 1;
printf("A\n");
yield();
}
void b(void) {
acc *= 2;
printf("B\n");
yield();
}
void c(void) {
acc += 1;
printf("C\n");
yield();
}

int main() {
quit = 0;
ntasks = 3;
task[0] = a;
task[1] = b;
task[2] = c;
loop();
return 0;
}

0 new messages