Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <traffic.h>
- #include <pthread.h>
- #define map_width 10
- #define map_height 10
- /* Nun die Kartendaten */
- static map_shorts map_def[]={
- R2,RH,RH,RH,RH,RH,RH,RH,RH,R3,
- RV,__,__,__,__,__,__,__,__,RV,
- RV,__,__,__,__,__,__,__,__,RV,
- RV,__,__,__,__,__,__,__,__,RV,
- RV,__,__,__,__,__,__,__,__,RV,
- RV,__,__,__,__,__,__,__,__,RV,
- RV,__,__,__,__,__,__,__,__,RV,
- RV,__,__,__,__,__,__,__,__,RV,
- RV,__,__,__,__,__,__,__,__,RV,
- R1,RH,RH,RH,RH,RH,RH,RH,RH,R4
- };
- pthread_cond_t bedingungsvariable;
- pthread_mutex_t mutex, mutex2;
- int makeAStop[5] = {-1, -1, -1, -1, -1};
- void drive(int arg[4]){
- directions dir[4] = {N, W, S, O};
- int x = arg[0], y = arg[1];
- int id = putTrain(x, y, dir[arg[2]], 0);
- int i = x<y?x+y:36-(x+y);
- int myIndex = arg[3];
- pthread_mutex_lock(&mutex2);
- makeAStop[myIndex] = (i-1)%36;
- pthread_mutex_unlock(&mutex2);
- if(i != 4)
- pthread_cond_wait(&bedingungsvariable, &mutex);
- while(1){
- for(; i<36; i++){
- int j;
- for(j=0;j<5;j++)
- if(i == makeAStop[j]){
- pthread_cond_signal(&bedingungsvariable);
- makeAStop[myIndex] = (i-1)%36;
- pthread_cond_wait(&bedingungsvariable, &mutex);
- }
- if(i < 8)
- moveTrain(id, S);
- else if(i == 8)
- moveTrain(id, SSO);
- else if(i < 17)
- moveTrain(id, O);
- else if(i == 17)
- moveTrain(id, OON);
- else if(i < 26)
- moveTrain(id, N);
- else if(i == 26)
- moveTrain(id, NNW);
- else if(i < 35)
- moveTrain(id, W);
- else
- moveTrain(id, WWS);
- }
- i = 0;
- }
- }
- int mainloop(void* args){
- pthread_cond_init(&bedingungsvariable, NULL);
- pthread_mutex_init(&mutex, NULL);
- pthread_mutex_init(&mutex2, NULL);
- int a[4] = {4, 0, 1, 0};
- int b[4] = {9, 4, 0, 1};
- int c[4] = {0, 4, 2, 2};
- int d[4] = {8, 9, 3, 3};
- int e[4] = {0, 3, 2, 4};
- pthread_t z1, z2, z3, z4, z5;
- pthread_create(&z5, NULL, &drive, &e);
- pthread_create(&z1, NULL, &drive, &a);
- pthread_create(&z2, NULL, &drive, &b);
- pthread_create(&z3, NULL, &drive, &c);
- pthread_create(&z4, NULL, &drive, &d);
- waitAndClose();
- return 0;
- }
- int main(){
- createMap(map_def, map_width, map_height, mainloop);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment