I can't find any evidence online pthread_cond_wait that was weird on Mac OS X, but it seems like a failure for the simplest test for me.
Function
int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t * );
should unlock the argument mutex # 2, and then wait for the signal to be sent in the argument of condition # 1. I wrote a simple program to check this, and also check for fake awakenings:
#include <stdio.h> #include <pthread.h> pthread_t spin_thread; pthread_mutex_t spin_mutex; pthread_cond_t spin_cond; int actual = 0; void *condspin( void *v ) { int expected = 0; for ( ;; ) { if ( actual != expected ) printf( "unexpected %d\n", actual ); else printf( "expected %d\n", actual ); pthread_mutex_lock( &spin_mutex ); printf( "locked\n" ); expected = actual + 1; pthread_cond_wait( &spin_cond, &spin_mutex ); } return NULL; } int main( int argc, char ** argv ) { pthread_mutex_init( &spin_mutex, NULL ); pthread_cond_init( &spin_cond, NULL ); pthread_create( &spin_thread, NULL, &condspin, NULL ); for ( ;; ) { getchar(); pthread_cond_signal( &spin_cond ); printf( "signaled\n" ); ++ actual; } return 0; }
But he only gets a lock once. The main thread does not even try to get a lock to just keep things going.
Shadow:~ dkrauss$ cc condwait.c -o condwait Shadow:~ dkrauss$ ./condwait expected 0 locked signaled expected 1 signaled signaled
If I add pthread_mutex_unlock after pthread_cond_wait , it will behave as expected. (Or, as expected, with just half of the locking mechanism.) So, what gives?
source share