Reputation: 691
I've been doing a program in C which uses a series of threads to represent cars crossing a bridge, the function I'm doing right know is the one that prints the "state" of the bridge at a certain moment, so it shows where cars (threads) are in the bridge and at which position. The problem is this; when there is only ONE thread crossing, the function prints properly and you can see the representation of the car advancing through the bridge. But when there are more threads crossing the function gets crazy and sometimes you can see a bridge and a half, or two bridges or some other crazy things. I tried to solve it creating a pthread_mutex for that function but it wasn't the solution, or at least I'm not using it at the right place. Every thread call this function as it advances through the bridge (the bridge is an array) Sorry if I failed to make it clear; here is the function and thanks for your time:
void printBridge(bridge *pBridge, int direction) {
system("clear");
int i;
if (direction == WEST) {
for (i = 0; i < bridgeSize; i++) {
pthread_mutex_lock(&print);
if (pBridge->cellState[i] == 0) { //this means the position at the array isn't occupied
fprintf(stderr, "\t");
} else if (pBridge->cellState[i] == 1) { //this means a car is at that position
fprintf(stderr, " @@@>"); //the cars are represented by @@@>
}
pthread_mutex_unlock(&print);
}
} else {
for (i = bridgeSize - 1; i >= 0; i--) { //if going the other direction
pthread_mutex_lock(&print);
if (pBridge->cellState[i] == 0) {
fprintf(stderr, "\t");
} else if (pBridge->cellState[i] == 1) {
fprintf(stderr, "<@@@ ");
}
pthread_mutex_unlock(&print);
}
}
printf("\n--------------------------------------------------------------------------\n");
fprintf(stderr, "\n Direction= %d", pBridge->direction);
sleep(1); //because I need to see what's going on
}
So...a proper print would be like this:
<@@@ <@@@
--------------------------------------------------------------------------
But sometimes it gets as messy as this:
<@@@ <@@@ <@@@ <@@@
--------------------------------------------------------------------------
<@@@ <@@@ <@@@ <@@@
Direction= 1--------------------------------------------------------------------------
Could it be because of the system("clear") being executed by many threads at a time?
SOLVED:
Solved calling the pthread_mutex_lock and unlock outside the function using a mutex for each position of the array ( bridge):
pthread_mutex_lock(&pBridge->mutexBridge[i]);
pBridge->cellState[i]=1;
printBridge(pBridge,dir);
pBridge->cellState[i]=0;
pthread_mutex_unlock(&pPuente->mutexBridge[i]);
And for some reason, inside the printBridge function, wrapped the system("clear") with mutex as well:
pthread_mutex_lock(&print);
system("clear");
pthread_mutex_unlock(&print);
Not doing the above gave me crazy prints. Thanks to pat for the help
Upvotes: 0
Views: 214
Reputation: 12749
You are only locking around printing of individual cars and spaces, so, when multiple threads are printing, they will interleave the bridges on a car by car basis. You need to acquire the mutex before you start printing the bridge and release it when you are done. In this way, threads will interleave whole bridges instead of individual cars.
Upvotes: 1