LukasHuhn

SI - Blatt8 A2

Oct 10th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <traffic.h>
  4. #include <pthread.h>
  5.  
  6.  
  7. #define map_width 5
  8. #define map_height 5
  9.  
  10. static map_shorts map_def[]={
  11.     S2,SH,S3,__,__,
  12.     SV,__,SV,__,__,
  13.     S1,SH,SC,SH,S3,
  14.     __,__,SV,__,SV,
  15.     __,__,S1,SH,S4
  16. };
  17.  
  18. static pthread_mutex_t kreuzung_mutex = PTHREAD_MUTEX_INITIALIZER;
  19.  
  20. void driveCircle(int arg[]){
  21.     int id = putCar(arg[0], arg[1], S, arg[2]);
  22.     map_position_t mp;
  23.     moveCar(id, S);
  24.  
  25.     int i;
  26.     for(i=0; 1; i++){
  27.  
  28.         moveCar(id, SSO);
  29.         moveCar(id, O);
  30.  
  31.         mp = routeCar(id);
  32.         if(mp.x == 2 && mp.y == 2){
  33.             pthread_mutex_lock(&kreuzung_mutex);
  34.             moveCar(id, OON);
  35.             moveCar(id, N);
  36.             pthread_mutex_unlock(&kreuzung_mutex);
  37.         }else{
  38.             moveCar(id, OON);
  39.             moveCar(id, N);
  40.         }
  41.  
  42.  
  43.  
  44.         moveCar(id, NNW);
  45.         moveCar(id, W);
  46.  
  47.         mp = routeCar(id);
  48.         if(mp.x == 2 && mp.y == 2){
  49.             pthread_mutex_lock(&kreuzung_mutex);
  50.             moveCar(id, WWS);
  51.             moveCar(id, S);
  52.             pthread_mutex_unlock(&kreuzung_mutex);
  53.         }
  54.         else{
  55.             moveCar(id, WWS);
  56.             moveCar(id, S);
  57.         }
  58.     }
  59. }
  60.  
  61. int mainloop(void* arg){
  62.     pthread_mutex_init(&kreuzung_mutex, NULL);
  63.  
  64.     pthread_t auto1, auto2, auto3, auto4;
  65.     int arg1[3] = {0, 0, 12};
  66.     int arg2[3] = {2, 2, 2};
  67.  
  68.     pthread_create(&auto1, NULL, &driveCircle, &arg1);
  69.     pthread_create(&auto2, NULL, &driveCircle, &arg2);
  70.     sleep(5);
  71.     pthread_create(&auto3, NULL, &driveCircle, &arg1);
  72.     sleep(1);
  73.     pthread_create(&auto4, NULL, &driveCircle, &arg2);
  74.  
  75.     pthread_join(auto1, NULL);
  76.     pthread_join(auto2, NULL);
  77.     pthread_join(auto3, NULL);
  78.     pthread_join(auto4, NULL);
  79.  
  80.     waitAndClose();
  81.     return 0;
  82. }
  83.  
  84. int main(){
  85.     createMap(map_def, map_width, map_height, mainloop);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment