// join-example.cc // Example of using Thread::Join(). #include "copyright.h" #include "system.h" #include "synch.h" int testnum = 1; void Joiner(Thread *joinee) { currentThread->Yield(); currentThread->Yield(); printf("Waiting for the Joinee to finish executing.\n"); currentThread->Yield(); currentThread->Yield(); // Note that, in this program, the "joinee" has not finished // when the "joiner" calls Join(). You will also need to handle // and test for the case when the "joinee" _has_ finished when // the "joiner" calls Join(). joinee->Join(); currentThread->Yield(); currentThread->Yield(); printf("Joinee has finished executing, we can continue.\n"); currentThread->Yield(); currentThread->Yield(); } void Joinee() { int i; for (i = 0; i < 5; i++) { printf("Smell the roses.\n"); currentThread->Yield(); } currentThread->Yield(); printf("Done smelling the roses!\n"); currentThread->Yield(); } void ForkerThread() { Thread *joiner = new Thread("joiner", 0); // will not be joined Thread *joinee = new Thread("joinee", 1); // WILL be joined // fork off the two threads and let them do their business joiner->Fork((VoidFunctionPtr) Joiner, (int) joinee); joinee->Fork((VoidFunctionPtr) Joinee, 0); // this thread is done and can go on its merry way printf("Forked off the joiner and joiner threads.\n"); } //---------------------------------------------------------------------- // ThreadTest // Invoke a test routine. //---------------------------------------------------------------------- void ThreadTest() { switch (testnum) { case 1: ForkerThread(); break; default: printf("No test specified.\n"); break; } }