Why does the following C program that uses pthreads only
work if I define the mutex to be a fast mutex. In this
particular case, if I use a nonrecursive mutex, the mutex
unlock routine returns a status of -1. Any ideas?
==========================================================
#include
#include
#include
static void mutex_init_c(pthread_mutex_t *mutex_handle,
int *status)
{
pthread_mutexattr_t attribute;
*status = pthread_mutexattr_create (&attribute);
*status = pthread_mutexattr_setkind_np (&attribute, MUTEX_NONRECURSIVE_NP);
if (*status == 0)
{
*status = pthread_mutex_init (mutex_handle, attribute);
}
return;
}
static int mutex_lock_c(pthread_mutex_t *mutex_handle)
{
int status = 0;
if (status = 0)
{
status = pthread_mutex_lock (mutex_handle);
}
return(status);
}
static int mutex_unlock_c(pthread_mutex_t *mutex_handle)
{
int status = 0;
status = pthread_mutex_unlock (mutex_handle);
return(status);
}main()
{
int status = 0;
pthread_mutex_t mutex;
mutex_init_c(&mutex, &status);
printf("The status for mutex init is : ");
printf("%d \n",status);
status = pthread_setasynccancel(CANCEL_OFF);
status = mutex_lock_c(&mutex);
printf("The status for mutex lock is : ");
printf("%d \n",status);
status = sleep(2);
status = mutex_unlock_c(&mutex);
printf("The status for mutex unlock is : ");
printf("%d \n",status);
}
> if (status = 0)
> {
> status = pthread_mutex_lock (mutex_handle);
> }
pthread_mutex_lock will never be called !
The code should probably have read 'if (status == 0)'.
I dont see the point of testing the value of status, as it has just
been set to 0 anyway.
I suspect that the mutex_unlock_c will appear to work for a fast mutex as
very few checks are made for fast mutexes, so it will not be noticed
that the mutex was not previously locked.