Subj : Condition variables & mutexes To : comp.programming.threads From : Ian Pilcher Date : Tue Jan 25 2005 09:59 am I'm making my first attempt to use a condition variable, and I'm struggling with how exactly I'm supposed to use the associated mutex. I finally got a simple example program to work (not hang): #include #include #include static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static void *thread(void *arg) { puts("thread: started"); pthread_mutex_lock(&mutex); puts("thread: mutex locked"); pthread_cond_signal(&cond); puts("thread: condition signalled"); pthread_mutex_unlock(&mutex); puts("thread: mutex unlocked"); return NULL; } int main(void) { pthread_t tid; puts("main: started"); pthread_mutex_lock(&mutex); puts("main: mutex locked"); pthread_create(&tid, NULL, thread, NULL); puts("main: thread created"); pthread_cond_wait(&cond, &mutex); puts("main: condition signalled"); pthread_join(tid, NULL); puts("main: thread joined"); return 0; } Is this basically correct, or is my use of the mutex "overkill"? Thanks! -- ======================================================================== Ian Pilcher i.pilcher@comcast.net ======================================================================== .