I am trying to simulate an intersection using threads and mutex locks.
I have functions for going strait, turn left, turn right.
Now, i have a function for approaching the intersection. This generates a random orientation and turn. Each thread shares the approaching intersection.
I have all the locks defined for all the cars in all directions.
Take the going strait function. It has a switch statement that just prints what car is doing what at the time. Now, i am just not sure what to lock in this function. If the car is in direction pointing north would i lock east and west and same with the car pointing south going north?
Here is my locks which just calls a function to lock or unlock
#define NUMCARS 30
#define lock_NW(CAR) lock(CAR, NW_mutex)
#define lock_NE(CAR) lock(CAR, NE_mutex)
#define lock_SW(CAR) lock(CAR, SW_mutex)
#define lock_SE(CAR) lock(CAR, SE_mutex)
#define unlock_NW(CAR) unlock(CAR, NW_mutex)
#define unlock_NE(CAR) unlock(CAR, NE_mutex)
#define unlock_SW(CAR) unlock(CAR, SW_mutex)
#define unlock_SE(CAR) unlock(CAR, SE_mutex)
here is main
int main(int argc, char **argv){
/* Initial variables*/
int index, tid;
unsigned int carids[NUMCARS];
pthread_t carthreads[NUMCARS];
/* Start up a thread for each car*/
for(index = 0; index <NUMCARS; index++){
carids[index] = index;
tid = pthread_create(&carthreads[index], NULL, approachintersection, (void*)&carids[index]);
}
/* Wait for every car thread to finish */
for(index = 0; index <NUMCARS; index++){
pthread_join(carthreads[index], NULL);
}
printf("Done\n");
return 1;
}
here is the approaching intersection which calls the function going strait
static void * approachintersection(void* arg){
unsigned int * carnumberptr;
unsigned int carnumber;
orientation_t cardir = (orientation_t)random()%4;
unsigned long turn = random()%3;
carnumberptr = (unsigned int*) arg;
carnumber = (unsigned int) *carnumberptr;
if(turn==LEFT){
turnleft(cardir, carnumber);
} else if(turn==RIGHT){
turnright(cardir, carnumber);
} else {//straight
gostraight(cardir, carnumber);
}
return (void*)carnumberptr;
}
Now, here is the going strait function where i want to lock the appropriate directions.
/*
cardirection - The direction the car is pointing. If it is pointing NORTH,
it is starting from the South-Eastern corner of the intersection
and "going straight" means it wants to move SOUTH to NORTH.
valid options: NORTH, SOUTH, EAST, WEST
carnumber - The car identifier
*/
static void gostraight(orientation_t cardirection, unsigned int carnumber){
switch(cardirection){
case NORTH:
printf("Car %d, Moving South-North\n", carnumber);
break;
case SOUTH:
printf("Car %d, Moving North-South\n", carnumber);
break;
case EAST:
printf("Car %d, Moving West-East\n", carnumber);
break;
case WEST:
printf("Car %d, Moving East-West\n", carnumber);
break;
}
}
So, if the approaching car is pointing north from south the car would be the SE car and i would lock case east, west print function with lock_SE(CAR)? preventing the other threads from coming in and printing? so i would lock unlock the print statements?
Or would i lock the whole switch statement?
** EDIT: would this be the way to do it? **
static void turnleft(orientation_t cardirection, unsigned int carnumber){
int CAR;
CAR = carnumber;
switch(cardirection){
case NORTH:
lock_SE(CAR)
printf("Car %d, Moving South-West\n", carnumber);
unlock_SE(CAR)
break;
case SOUTH:
lock_NW(CAR)
printf("Car %d, Moving North-East\n", carnumber);
unlock_NW(CAR)
break;
case EAST:
lock_SW(CAR)
printf("Car %d, Moving West-North\n", carnumber);
unlock_SW(CAR)
break;
case WEST:
lock_NE(CAR)
printf("Car %d, Moving East-South\n", carnumber);
unlock_NE(CAR)
break;
}
}
This is not an easy problem. I’ll try to show two solutions.
First the obvious one: One mutex for the entire intersection, in the beginning of
turnleft, turnright, gostraightaddlock(car, intersection_mutex);, just before the end of each function release said mutex. This will only let one car in through the intersection at a time. The upside of this is that it’s easy to understand and will not result in dead locks. The downside is that a only one car can enter at a time but as we all know two cars travelling non-intersecting paths can enter without issues.Here’s an example of
go_straight()(the others follow the same approach):To let more than one car in at a time we need a fine grained approach. The problem with a fine grained approach is that it’s much harder to implement and get correct. Both
go_straightandturn_leftneeds to lock two mutexes (you could argue that turn left needs three..). So if you can’t acquire both mutexes you need to back off. Translating this into driving rules:So, to go straight we must first acquire the mutex nearest to you, then the next one in your path to be able to exit. If we can’t get both we must release the one we have locked. If we don’t release it we will dead-lock.
To do this I’d add two helper functions:
Here’s my version of go straight:
turn_leftwould then need to follow the same approach.