📄 threadtest_ex2.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 "system.h"#include "dllist.h"#include "synch-sleep.h"#include "BoundedBuffer.h"#include "Table.h"// testnum is set in main.cc#define Maxsize 20int testnum = 1;DLList *A=new DLList();Table *A2 = new Table(30);Lock* lock = new Lock("list lock");Condition* listEmpty = new Condition("list empty cond");BoundedBuffer* buffer = new BoundedBuffer(Maxsize);//----------------------------------------------------------------------// SimpleThread// Loop 5 times, yielding the CPU to another ready thread // each iteration.//// "which" is simply a number identifying the thread, for debugging// purposes.//----------------------------------------------------------------------extern int T,N,P;void SimpleThread1(int which){ int num; num=1; while(1){ printf("*** thread %d looped %d times\n", which, num); num++; driver_insert(A,N,which); currentThread->Yield(); A->Print(); driver_remove(A,N,which); if(A->IsEmpty()||num==5) break; }}//----------------------------------------------------------------------// ThreadTest1// Set up a ping-pong between two threads, by forking a thread // to call SimpleThread, and then calling SimpleThread ourselves.//----------------------------------------------------------------------void SimpleThread2(int which){ int num = 0,i; char object[20], *obj; while(1) { printf("*** thread %d looped %d times ***\n",which,num); ++num; switch(which){ case 1: strcpy(object,"thread 1");break; case 2: strcpy(object,"thread 2");break; case 3: strcpy(object,"thread 3");break; default: object[0] = '\0'; } i = A2->Alloc(object); obj = (char *)A2->Get(i); printf("(thread %d)index = %d :%s\n",which,i,obj); currentThread->Yield(); obj = (char *)A2->Get(i); printf("(thread %d)index = %d :%s\n",which,i,obj); currentThread->Yield(); if(num == 2) break; }}void SimpleThread3(int which){ int num,length,i; char read[100],write[100]; num=1; while(1){ i = 0; while(i<100) read[i++] = '\0'; printf("*** thread %d looped %d times\n", which, num); num++; if(which%2 == 0) { buffer->Read(read,5); printf("thread %d read %s\n",which,read); buffer->printbuffer(); currentThread->Yield(); } strcpy(write,"woshishui"); length = strlen(write); buffer->Write(write,length); printf("thread %d write %s\n",which,write); buffer->printbuffer(); currentThread->Yield(); if(which%2 == 1) { buffer->Read(read,5); printf("thread %d read %s\n",which,read); buffer->printbuffer(); currentThread->Yield(); } if(num==3) break; }}voidThreadTest1(){ int i; DEBUG('t', "Entering ThreadTest1"); for(i=1;i<=T;i++){ Thread *t1 = new Thread("forked thread"); switch(P) { case 1:t1->Fork(SimpleThread1,i);break; case 2:t1->Fork(SimpleThread2,i);break; case 3:t1->Fork(SimpleThread3,i);break; } }}//----------------------------------------------------------------------// ThreadTest// Invoke a test routine.//----------------------------------------------------------------------voidThreadTest(){ switch (testnum) { case 1: ThreadTest1(); break; default: printf("No test specified.\n"); break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -