Created
December 7, 2016 16:37
-
-
Save harveyslash/1a2fa83975ab8f7c800caf30c11c50e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _BSD_SOURCE | |
#include <time.h> | |
#include <string.h> | |
#include "racer.h" | |
#include "display.h" | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
long maxSpeedDelay ; | |
void initRacers( long milliseconds ){ | |
srand(time(NULL)); | |
if(milliseconds<0) maxSpeedDelay = DEFAULT_WAIT; | |
else | |
maxSpeedDelay = milliseconds; | |
} | |
char * getfullCar(char * name,int isflat){ | |
char *car = malloc(sizeof(char)*(MAX_CAR_LEN+1)); | |
sprintf(car,"~%c=-------o>",isflat==1?'X':'O'); | |
for(int i =0;i<strlen(name);i++){ | |
car[i+3] = name[i]; | |
} | |
return car; | |
} | |
Racer *makeRacer( char *name, int position ){ | |
Racer *racer = (Racer *)malloc(sizeof(Racer)); | |
racer->dist = 0; | |
racer->row = position; | |
racer->graphic = getfullCar(name,0); | |
return racer; | |
} | |
void outputCar(Racer *racer){ | |
for(int i=0;i<MAX_CAR_LEN;i++){ | |
set_cur_pos(racer->row,racer->dist + i); | |
put(racer->graphic[i]); | |
} | |
} | |
void clearTail(Racer *racer){ | |
if(racer->dist==0)return; | |
set_cur_pos(racer->row,racer->dist -1); | |
put(' '); | |
} | |
void destroyRacer( Racer *racer ){ | |
free(racer->graphic); | |
free(racer); | |
} | |
pthread_mutex_t count_mutex; | |
void *run( void *racer ){ | |
Racer *myracer = (Racer *)racer; | |
int flatTire = 0; | |
while(myracer->dist <= FINISH_LINE){ | |
pthread_mutex_lock(&count_mutex); | |
outputCar(myracer); | |
clearTail(myracer); | |
pthread_mutex_unlock(&count_mutex); | |
myracer->dist++; | |
int speedDelay = rand()%maxSpeedDelay; | |
if(speedDelay <= 3){ | |
myracer->graphic[1]='X'; | |
break; | |
} | |
usleep(rand()%speedDelay); | |
} | |
/*return 0;*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include "racer.h" | |
#include "display.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> /* for strerror() */ | |
#define NUM_THREADS 5 | |
void *PrintHello(void *threadid) | |
{ | |
long tid; | |
tid = (long)threadid; | |
printf("Hello World! It's me, thread #%ld!\n", tid); | |
pthread_exit(NULL); | |
} | |
int main (int argc, char *argv[]) | |
{ | |
clear(); | |
initRacers(500000); | |
Racer *racecar = makeRacer("harsh",3); | |
Racer *racecar2 = makeRacer("khant",6); | |
pthread_t threads[2]; | |
pthread_create(&threads[0],NULL,run,(void *)racecar); | |
pthread_create(&threads[1],NULL,run,(void *)racecar2); | |
pthread_join(threads[0],NULL) ; | |
pthread_join(threads[1],NULL) ; | |
set_cur_pos(7,0); | |
return 0; | |
/*pthread_t threads[NUM_THREADS];*/ | |
/*int rc;*/ | |
/*long t;*/ | |
/*for(t=0; t<NUM_THREADS; t++){*/ | |
/*printf("In main: creating thread %ld\n", t);*/ | |
/*rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);*/ | |
/*if (rc){*/ | |
/*printf("ERROR; return code from pthread_create() is %d\n", rc);*/ | |
/*exit(-1);*/ | |
/*}*/ | |
/*}*/ | |
/*pthread_exit(NULL);*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment