From: Hongli Lai Date: 2008-03-05T20:08:06+09:00 Subject: Re: embedding Ruby 1.9.0 inside pthread Suraj N. Kurapati wrote: > Thanks for the tip. I revised the example accordingly: This is better, but now your program has a racing condition. Suppose the thread finished before your pthread_cond_wait() is called. Now you're waiting for a condition that will never be signalled. The correct way to wait for a condition is to use the following pattern: pthread_mutex_t lock; pthread_cond_t cond; void function_1() { pthread_mutex_lock(&lock); while (!"some expression which must evaluate to true") { pthread_cond_wait(&lock, &cond); } pthread_mutex_unlock(&lock); } void function_2() { pthread_mutex_lock(&lock); (...do some work, which makes "some expression which must evaluate to true" true...) pthread_cond_signal(&cond); // use broadcast() if there can be multiple threads waiting on this condition pthread_mutex_unlock(&lock); }