📄 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 Insertlist(int n,DLList *list,int which);
extern void Insertlist_new(int n,DLList *list,int which);
extern void Removelist(int n,DLList *list,int which);
extern void Removelist_new(int n,DLList *list,int which);
char choose;
// testnum is set in main.cc
int testnum = 1;
DLList *list;
//extern DLList list;
//----------------------------------------------------------------------
// SimpleThread
// Loop 5 times, yielding the CPU to another ready thread
// each iteration.
//
// "which" is simply a number identifying the thread, for debugging
// purposes.
//----------------------------------------------------------------------
void
SimpleThread(int which)
{
switch(choose)
{
case '1':
Insertlist(3,list,which);
Removelist(3,list,which);
break;
case '2':
Insertlist_new(3,list,which);
Removelist_new(3,list,which);
break;
}
// printf("*** thread %d \n", which);
currentThread->Yield();
}
//----------------------------------------------------------------------
// ThreadTest1
// Set up a ping-pong between two threads, by forking a thread
// to call SimpleThread, and then calling SimpleThread ourselves.
//----------------------------------------------------------------------
void
ThreadTest1()
{
DEBUG('t', "Entering ThreadTest1");
Thread *t = new Thread("forked thread");
t->Fork(SimpleThread, 1);
SimpleThread(0);
}
//----------------------------------------------------------------------
// ThreadTest
// Invoke a test routine.
//----------------------------------------------------------------------
void
ThreadTest()
{
while(1)
{
printf("1.未加锁的演示结果\n2.加锁后的演示结果\n");
choose = getchar();
if(choose != '1' && choose !='2')
printf("error\n");
else break;
}
switch (testnum) {
case 1:
list=new DLList();
ThreadTest1();
break;
default:
printf("No test specified.\n");
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -