I am currently learning C from MIT\'s Open Courseware course called Practical Programming in C. In discussing race conditions in multithreading, the lecture notes contained
The reason why it works with linux is, because under linux :
#define PTHREAD_MUTEX_INITIALIZER { { 0, 0, 0, 0, 0, { 0 } } }
which is exactly how the mutex is initialized anyway, since it is a .bss-variable.
Under MacOSX, it is some other magic value:
#define PTHREAD_MUTEX_INITIALIZER {_PTHREAD_MUTEX_SIG_init, {0}}
#define _PTHREAD_MUTEX_SIG_init 0x32AAABA7
so it indeed has to be initialized to have it working correctly.
You didn't initialize the mutex. You can either do this:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
or in the main
function, do this:
if ( pthread_mutex_init( &mutex, NULL) != 0 )
printf( "mutex init failed\n" );