Subj : Re: why my threads do not wake up To : comp.programming.threads From : David Schwartz Date : Thu Aug 18 2005 01:04 pm "Jingbo" wrote in message news:1124384707.985991.201890@g14g2000cwa.googlegroups.com... > I created 8 threads to do some tasks in parallel. I ran my program on a > SunFire which has 8 CPUs. I found that only 2 or 3 threads are working > on their tasks. Other threads are waiting in pthread_cond_wait(). I > print a message after pthread_cond_wait(), and found out they are never > waken up. > > I'm sure that there are plenty of jobs on the queue and > pthread_cond_broadcast() is called and there is no other processes > running on the server therefore CPUs are idel and sometimes my program > can use all 8 CPUs. > > My question is, why Solaris did not put my threads on CPU even if there > are idel CPUs? Because the threads are waiting for a condition to be signalled. You called 'pthread_cond_wait' and that will cause your threads to wait until some other thread signals the condition variable. Now, if you had said, "I print a message after pthread_cond_broadcast, and found out they are never waking up", you'd have something. That's the function that's supposed to wake them up. Condition variables are hard to use correctly until you get the hang of it. If your call to 'pthread_cond_wait' doesn't look like this: pthread_mutex_lock(...) while(nothing_to_do()) pthread_cond_wait(...) do_stuff(); pthread_mutex_unlock(); Then that's your problem. Most importantly, don't wait if there's work to do! DS .