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 5
- #define map_height 5
- static map_shorts map_def[]={
- S2,SH,S3,__,__,
- SV,__,SV,__,__,
- S1,SH,SC,SH,S3,
- __,__,SV,__,SV,
- __,__,S1,SH,S4
- };
- static pthread_mutex_t kreuzung_mutex = PTHREAD_MUTEX_INITIALIZER;
- void driveCircle(int arg[]){
- int id = putCar(arg[0], arg[1], S, arg[2]);
- map_position_t mp;
- moveCar(id, S);
- int i;
- for(i=0; 1; i++){
- moveCar(id, SSO);
- moveCar(id, O);
- mp = routeCar(id);
- if(mp.x == 2 && mp.y == 2){
- pthread_mutex_lock(&kreuzung_mutex);
- moveCar(id, OON);
- moveCar(id, N);
- pthread_mutex_unlock(&kreuzung_mutex);
- }else{
- moveCar(id, OON);
- moveCar(id, N);
- }
- moveCar(id, NNW);
- moveCar(id, W);
- mp = routeCar(id);
- if(mp.x == 2 && mp.y == 2){
- pthread_mutex_lock(&kreuzung_mutex);
- moveCar(id, WWS);
- moveCar(id, S);
- pthread_mutex_unlock(&kreuzung_mutex);
- }
- else{
- moveCar(id, WWS);
- moveCar(id, S);
- }
- }
- }
- int mainloop(void* arg){
- pthread_mutex_init(&kreuzung_mutex, NULL);
- pthread_t auto1, auto2, auto3, auto4;
- int arg1[3] = {0, 0, 12};
- int arg2[3] = {2, 2, 2};
- pthread_create(&auto1, NULL, &driveCircle, &arg1);
- pthread_create(&auto2, NULL, &driveCircle, &arg2);
- sleep(5);
- pthread_create(&auto3, NULL, &driveCircle, &arg1);
- sleep(1);
- pthread_create(&auto4, NULL, &driveCircle, &arg2);
- pthread_join(auto1, NULL);
- pthread_join(auto2, NULL);
- pthread_join(auto3, NULL);
- pthread_join(auto4, NULL);
- waitAndClose();
- return 0;
- }
- int main(){
- createMap(map_def, map_width, map_height, mainloop);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment