1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// 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 "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "copyright.h"
#include "system.h"
#include "list.h"
//#include "player.h"
//----------------------------------------------------------------------
// SimpleThread
// Loop 5 times, yielding the CPU to another ready thread
// each iteration.
//
// "which" is simply a number identifying the thread, for debugging
// purposes.
//----------------------------------------------------------------------
#ifndef PLAYER_H
#define PLAYER_H
#include "copyright.h"
class Player{
int skill;
char favGame;
public:
~Player(void);
Player(void);
void setGame(char);
char getGame(void);
void setSkill(int);
int getSkill(void);
void printSkill(void);
};
#endif
Player::Player(void){
favGame = 'a';
skill = 1;
//printf("skill = %d \n", &skill);
}
Player::~Player(void){
//printf("hello again \n");
}
void Player::setGame(char a){
favGame = a;
}
char Player::getGame(void){
return favGame;
}
void Player::setSkill(int i){
skill = i;
}
int Player::getSkill(void){
return skill;
}
void Player::printSkill(void){
printf("skill = %d \n", skill);
}
void
SimpleThread(int which)
{
int num;
for (num = 0; num < 5; num++) {
printf("*** thread %d looped %d times\n", which, num);
currentThread->Yield();
}
}
//----------------------------------------------------------------------
// ThreadTest
// Set up a ping-pong between two threads, by forking a thread
// to call SimpleThread, and then calling SimpleThread ourselves.
//----------------------------------------------------------------------
void
ThreadTest()
{
DEBUG('t', "Entering SimpleTest");
Thread *t = new Thread("forked thread");
printf("Hi \n");
int count = 1;
int a;
char c;
List *pokemon;
List *finalFantasy;
List *pacMan;
List *legendZelda;
Player *players[15];
for(count = 0; count < 15; count ++){
players[count] = new Player();
}
//random seed
srand( time(NULL));
//for loop which assigns a skill and favorite game to all 15 players
for( count = 0; count < 15; count++){
players[count]->setSkill( rand() %10+1);
a = rand() %4+1;
switch(a){
case 1:
c = 'a';
pokemon->Append(&players[count]);
break;
case 2:
c = 'b';
finalFantasy->Append(&players[count]);
break;
case 3:
c = 'c';
pacMan->Append(&players[count]);
break;
case 4:
c = 'd';
legendZelda->Append(&players[count]);
break;
default:
break;
}
players[count]->setGame(c);
}
printf("hello \n");
pokemon = new List;
finalFantasy = new List;
pacMan = new List;
legendZelda = new List;
for(count = 0; count < 15; count++){
printf("Player %d ticket = %c%d \n", count, players[count]->getGame(), players[count]->getSkill());
}
t->Fork(SimpleThread, 1);
SimpleThread(0);
}
|