#include #include using namespace std; struct process; struct process; struct count_to_n_data; typedef int (*process_func)(process *p); struct process; struct process { process_func f; int pc; void *data; }; struct count_to_n_data { int n; int c; }; int count_to_three(process *p); int count_to_n(process *p); int main(void); int count_to_three(process *p) { switch(p->pc) { case 0: printf("1\n"); return 1; case 1: printf("2\n"); return 2; case 2: printf("3\n"); return 3; case 3: ; } return -1; } int count_to_n(process *p) { count_to_n_data *d = (count_to_n_data *)p->data; switch(p->pc) { case 0: d->c = 1; while(d->c <= d->n) { printf("%d\n", d->c); ++ (d->c); return 1; case 1: ; } } return -1; } int main(void) { process c3 = { count_to_three, 0 }; while(c3.pc != -1) { c3.pc = (*c3.f)(&c3); } count_to_n_data d1, d2; process cn1 = { count_to_n, 0, &d1 }; process cn2 = { count_to_n, 0, &d2 }; d1.n = 10; d2.n = 15; deque q; q.push_back(&cn1); q.push_back(&cn2); while(!q.empty()) { process *p = q.front(); q.pop_front(); int pc = (*p->f)(p); if(pc != -1) { p->pc = pc; q.push_back(p); } } return 0; }