/**** * Example 1: A Traditional DynC Cooperatively Multithreaded Program ****/ main(void) { /* CoData structures for two named tasks */ CoData t1, t2; while(1) { /* Keyboard handler */ costate { while(1) { waitfor(kbhit()); switch(getchar()) { case '-': /* Stop one of the currently running tasks */ if (isCoRunning(&t1)) CoPause(&t1); else if (isCoRunning(&t2)) CoPause(&t2); else printf("No tasks running.\n"); break; case '+': /* Start one of the currently stopped tasks */ if (!isCoRunning(&t1)) CoResume(&t1); else if (!isCoRunning(&t2)) CoResume(&t2); else printf("Both tasks running.\n"); break; } } } /* Task 1 (initially on) */ costate t1 init_on { while (1) { /* Task 1 work goes here. */ printf("Task 1\n"); yield; } } /* Task 2 (initially off) */ costate t2 { while (1) { /* Task 2 work goes here. */ printf("Task 2\n"); yield; } } } }