www.pudn.com > b048008.rar > threadtest.cc


// 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  
#include "system.h" 
#include "dllist.h" 
#include "EventBarrier.h" 
#include "Alarm.h" 
//#include "Elevator.h" 
 
int testnum = 1; 
int threadnum = -1; 
int casenum = 0; 
extern Alarm *alarmclock; 
 
EventBarrier *test = new EventBarrier("EventBarrier test"); 
DLList *list; 
//Building *building = new Building("building",15,20); 
 
 
 
 
void  SimpleThread_1(int which) 
{ 
    if(which == 3) 
    { 
        printf("Thread %d comes\nThread %d create a event.\n",which,which); 
        threadnum = which; 
        test->Signal(); 
        printf("Thread %d over.\n",which); 
    } 
    else 
    { 
        printf("Thread %d comes.\n",which); 
        threadnum = which; 
        test->Wait(); 
        threadnum = which; 
        printf("Thread %d wake up.\n",which); 
        test->Complete(); 
        threadnum = which; 
        printf("Thread %d over.\n",which); 
    } 
} 
 
void SimpleThread_2(int which) 
{ 
    threadnum = which; 
    printf("Thread %d before pause\n",which); 
    alarmclock->Pause(5*(which+1)); 
    printf("Thread %d after pause\n",which); 
} 
 
void ThreadTest1() 
{ 
    DEBUG('t', "Entering ThreadTest3_1"); 
    int fork_num=4; 
    while(fork_num!=0) 
   { 
    Thread *t=new Thread("forked thread"); 
    t->Fork(SimpleThread_2,5-fork_num); 
    fork_num--; 
   } 
    SimpleThread_2(0); 
} 
 
 
void 
ThreadTest() 
{     
switch (testnum) { 
    case 1: 
             list=new DLList(); 
         ThreadTest1(); 
    break; 
    default: 
    printf("No test specified.\n"); 
    break; 
    } 
}