threadtest_ex1.cc
来自「linux的例子,就是下载后到自己的机子上去运行」· CC 代码 · 共 82 行
CC
82 行
// 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"// testnum is set in main.ccint testnum = 1;DLList *A=new DLList();//----------------------------------------------------------------------// 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;voidSimpleThread(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(); driver_print(A); driver_remove(A,N,which); if(A->IsEmpty()) break; }}//----------------------------------------------------------------------// ThreadTest1// Set up a ping-pong between two threads, by forking a thread // to call SimpleThread, and then calling SimpleThread ourselves.//----------------------------------------------------------------------voidThreadTest1(){ int i; DEBUG('t', "Entering ThreadTest1"); for(i=1;i<=T;i++){ Thread *t1 = new Thread("forked thread"); t1->Fork(SimpleThread,i); }}//----------------------------------------------------------------------// 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 + =
减小字号Ctrl + -
显示快捷键?