Subj : Re: pthread problems: wake up a thread via pthread_cond_signal() To : comp.programming.threads From : David Schwartz Date : Sun Jan 02 2005 03:21 pm > for(i=0; i<2; i++){ > in_err = pthread_create(&in_thr[i], NULL, (void *(*)(void > *))thr1, (void *)i); > if (in_err) > exit (1); > } > > out_err = pthread_create(&out_t2, NULL, (void *(*)(void *))thr2, > NULL); > if (out_err) > exit (1); > > > return (0); > } You fall off the end of 'main'. You need to join all your threads. > pthread_mutex_lock(&lock[id]); > > if(status[id] == 1){ > printf("wait %i\n", id); > pthread_cond_wait(&in_turn[id], &lock[id]); > } > > > pthread_mutex_lock(&status_lock); > work[id] = 1; > pthread_mutex_unlock(&status_lock); Notice that 'work[id]' can only be set to '1' by a thread that holds the 'lock[id]' mutex. > pthread_mutex_lock(&lock[i]); > > printf("---- write data from thread %i ---\n", i); > > pthread_mutex_lock(&status_lock); > status[i] = 0; > pthread_mutex_unlock(&status_lock); > > printf("(int)status[i] %i\n", status[i]); > printf("(int)work[i] %i\n", work[i]); > > > while((int)work[i] != 1){ > pthread_cond_signal(&in_turn[i]); > printf("## signal to thread %i\n", i); > } So how is another thread going to set 'work[i]' to 1? It would have to hold the 'lock[i]' mutex, and *this* thread holds that mutex. DS .