📄 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 "system.h"#include "dllist.h"extern void insert(DLList*list,int n);extern void remove(DLList*list,int n);extern DLList*list;extern int mode;// testnum is set in main.ccint testnum = 1;//----------------------------------------------------------------------// SimpleThread// Loop 5 times, yielding the CPU to another ready thread // each iteration.//// "which" is simply a number identifying the thread, for debugging// purposes.//----------------------------------------------------------------------voidSimpleThread(int which){ int num; for (num = 0; num < 1; num++) { printf("*** thread %d looped %d times ***\n", which, num); if(mode==1){ remove(list,1); //printf("items in the list:"); list->Print(); //printf("\n"); //currentThread->Yield(); // remove(list,2); // list->Print(); // printf("\n");} } else if(mode==2||mode==3) { int *itemPtr; printf("thread %d find the location to insert %d\n",which,50+which); if(which==0){ list->SortedInsert(itemPtr,50); printf("after thread %d insert %d\n",which,50+which); list->Print();} else if(which==1){ list->SortedInsert(itemPtr,51); printf("after thread %d insert %d\n",which,50+which); list->Print();} } currentThread->Yield(); }}//----------------------------------------------------------------------// ThreadTest1// Set up a ping-pong between two threads, by forking a thread // to call SimpleThread, and then calling SimpleThread ourselves.//----------------------------------------------------------------------voidThreadTest1(){ DEBUG('t', "Entering ThreadTest1"); Thread *t = new Thread("forked thread"); t->Fork(SimpleThread, 1); SimpleThread(0);}//----------------------------------------------------------------------// 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 + -