// threadtest.cc // Simple test case for the threads assignment. // // Create two threads, and have them context switch // back and forth between themselves by calling Thread::Yield, // to illustratethe inner workings of the thread system. // // Copyright (c) 1992-1993 The Regents of the University of California. // All rights reserved. See copyright.h for copyright notice and limitation // of liability and disclaimer of warranty provisions. #include "copyright.h" #include "system.h" #include "synch.h" #include using namespace std; //---------------------------------------------------------------------- // SimpleThread // Loop 5 times, yielding the CPU to another ready thread // each iteration. // // "which" is simply a number identifying the thread, for debugging // purposes. //---------------------------------------------------------------------- Semaphore c("SemaforoC",0); Semaphore m("SemaforoM",0); int ms = 0; int cs = 0; void SimpleThread(int which) { int num; if(which == 0){ cout << "Soy misionero" << endl; if(ms >= 1 && cs >= 1){ m.V(); c.V(); ms -= 1; cs -= 1; cout << "Cruzar!!!" << endl; cout<<"misioneros esperando = "<= 2){ m.V(); m.V(); ms -= 2; cout << "Cruzar!!!" << endl; cout<<"misioneros esperando = "<Fork(SimpleThread, random); } SimpleThread(0); }