news@dartvax.Dartmouth.EDU (USENET News System) (05/16/88)
What routines would you use to implement co-routines, that is, routines that execute co-operatively. ? How would you arrange for interprocess communications (again, within one instance of a program) between these co-routines? Currently, whenever my program is waiting for an event to happen (specifically for the moment, a keypress), it checks if the keyboard is ready (no redirection, here). If not, it calls multitask(), a simple little routine that dispatches calls to co-routines (that do not depend upon other events like the keyboard). Example: main() { printf("Press any key to continue..."); { char c; c=xgetch(); } printf("\n"); } From: major@eleazar.dartmouth.edu (Lou Major) Path: eleazar.dartmouth.edu!major char xgetch() { while (! _bios_keybrd(_KEYBRD_READY)) multitask(); return _bios_keybrd(_KEYBRD_READ); } /* above routine ignores the fact that extended keycodes are passed in the upper byte of the return value from _bios_keybrd This is very IBM-PC specific code, however the general idea is still applicable */ void multitask() { putclock(); /* other little things like a bouncing ball on one line of the screen */ } void putclock() { /* code to make Monday, January 1, 1980 12:01:00 am */ } this works fine for me in practice, although it requires that I make no calls to the built-in routines getc() and getch()a nd getche() (or any other "blocking" routines, that is, ones that wait for events from the keyboard). I am willing to work around this little idiosyncrasy. Also, I simplified my multitask() routine a little. In actuality, another routine is called with the address of a void function taking 0 arguments (eg, void putclock(void); ), and a linked list is built containing routines to execute on each instantiation of multitask(). These functions (putclock(), for example) check to see if they have any action to do (if the time has changed to the next integral second), and if not, merely return rather quickly. My multitask function gets called about 5,600 times per second while waiting for a keypress (on a 6Mhz PC-AT). I have posted this to comp.lang.C (instead of comp.sys.ibm.pc.d) as this is really a C question, not an IBM question. THank you for your help in this matter. Lou Major major@eleazar.darmtouth.edu major@eleazar.dartcms1.bitnet