⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadtest.cc

📁 本次实验的目的在于掌握使用nachos中的线程序解决较为复杂的并发问题。实验内容分三部分:实现事件栅栏原语并进行正确性测试;实现闹钟原语并进行正确性测试;利用事件栅栏和闹钟原语来解决电梯问题(详细内容
💻 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 <iostream.h>
#include "system.h"
#include "dllist.h"
#include "EventBarrier.h"
#include "Alarm.h"
//#include "Elevator.h"

int testnum = 1;
int threadnum = -1;
int casenum = 0;

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 ThreadTest1()
{
    DEBUG('t', "Entering ThreadTest3_1");
    int fork_num=4;
    while(fork_num!=0)
   {
    Thread *t=new Thread("forked thread");
    t->Fork(SimpleThread_1,5-fork_num);
    fork_num--;
   }
    SimpleThread_1(0);
}


void
ThreadTest()
{    
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 + -