I'm trying to use mutex instead of semaphore because I want semaphore behavior but binary (not counting). (Perhaps you'll notice I'm in the early stages of trying to simulate the Sleeping Barber algorithm.) This is my code:
#include <stdio.h>
#include <pthread.h>
int main( int argc, char** argv ) {
int freeSeats = 6;
pthread_mutexattr_t mutexAttr;
pthread_mutex_t custWaiting, wrAccess, barberReady;
pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&custWaiting, &mutexAttr);
pthread_mutex_init(&wrAccess, &mutexAttr);
pthread_mutex_init(&barberReady, &mutexAttr);
pthread_mutex_lock(&custWaiting);
pthread_mutex_lock(&custWaiting);
pthread_mutex_lock(&custWaiting);
fprintf(stdout, "got here\n\n");
return 0;
}
When I execute for the first time, this runs as expected (thread is blocked, command line hangs while my program waits to be able to lock). When I kill the program, and run it a second time, it prints "got here", which it should not. Why would this only fail on the second (and all following) attempts, but not on the first?
Bafflingly, if I modify the code as follows (only the init and lock lines):
#include <stdio.h>
#include <pthread.h>
int main( int argc, char** argv ) {
int freeSeats = 6;
pthread_mutexattr_t mutexAttr;
pthread_mutex_t custWaiting, wrAccess, barberReady;
pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED);
int y = pthread_mutex_init(&custWaiting, &mutexAttr);
y = pthread_mutex_init(&wrAccess, &mutexAttr);
y = pthread_mutex_init(&barberReady, &mutexAttr);
int x = pthread_mutex_lock(&custWaiting);
x = pthread_mutex_lock(&custWaiting);
x = pthread_mutex_lock(&custWaiting);
fprintf(stdout, "got here\n\n");
return 0;
}
... then it works every time. The reason this is so maddening is because I can't check error codes on pthread_mutex_whatever() because it doesn't fail when I try to catch the error codes. I don't want to assign the return values to int
s if I'm not going to use them. As you can see, I'm not using x
or y
at all; just assigning return values of init
and lock
functions to them. So why does that change the behavior of the mutexes so drastically? Or am I missing something else? What am I doing wrong?