Subj : Re: many producers/one consumer To : comp.programming.threads From : Bluejack Date : Thu Feb 17 2005 01:49 pm On Thu, 17 Feb 2005 13:56:48 -0500, Lankad wrote: > Now, what I don't like about this, is the fact that I have a while loop > that > keeps checking the counter and an if statement that keeps checking if > there > is a job. I'd love to use some other synchronization mechanism that can > stop > unneccessary checking of jobs... so that once the producers put a job on > the > queue, they can notify the consumer to work on all the jobs in the queue. So there are two pieces of information you are interested in: (1) There's a new job on the queue. (2) All producers have exitted. Despite Eric Sosman's compelling arguments, I still like the idea of a counter. It would work particularly well in conjunction with a pthread_cond_wait synchronization mechanism. (See http://www.devdaily.com/unix/man/man3/pthread_cond_init.3thr.shtml or http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#ConditionVariables ) Your algorithm could quite simply be: * producer -> (1) add job to queue, (2) pthread_cond_signal() (3) if more work, loop to (1) (4) if not, decrement producer counter (5) pthread_cond_signal (6) pthread_exit * consumer -> (1) pthread_cond_wait() (2) check queue (3) if job, consume job, loop to (2) (4) if no job, (5) check counter: if counter == 0, pthread_exit (6) if counter > 0 loop to (1) The key point in this design is who locks the condition mutex when, which, once you grasp the concept, should be straightforward. As with any interaction between threads, there are a couple of possible race conditions that will require correct locking of shared data. The good thing about this design is there is neither latency nor cpu thrashing as some consumer loops to find a change in state. The *right* solution will vary according to the requirements of the application and, as Eric mentioned, the eye of the beholder. -bluejack .