a21 Subj : Re: many producers/one consumer To : comp.programming.threads From : Eric Sosman Date : Thu Feb 17 2005 01:54 pm Bluejack wrote: > On Thu, 17 Feb 2005 11:21:25 -0500, Eric Sosman > wrote: > > >>>What is a good algorithm to handle this sort of thing. The thing that's >>>bugging me is when the producers finish producing data and exit, I want >>>the >>>consumer to know that it should stop checking for new data and exit. >> >> One approach is for each producer to put a final >>"Hail and farewell" item on the end of the queue just >>before it exits. When the consumer finds this special >>item it can decrement its N_producers counter and exit >>if it's reached zero. > > > To avoid unnecessary complications of data structures or data > semantics, my inclination would be to share a counter among > all producers and consumers. A producer thread, when started, > increments the counter. When it exits, it decrements the counter. > The consumer can periodically check to see if the counter is > zero, and exit if so. This works fine, but it doesn't actually eliminate the complications; it just moves them from one place to another (and, IMHO, magnifies them a little). Scenario: All producers but one have exited, the queue is empty, and the consumer is waiting for something to be added to the empty queue. Now the final producer decides to exit: it decrements the counter and does so. Result: the consumer is still waiting for something to show up on the queue and does not see the now-zero counter; the program is hung. Checking the counter "periodically" solves the problem, but at the cost of forcing the consumer to awaken now and then simply to check the counter -- pthread_cond_timedwait() instead of pthread_cond_wait(), for example. But this incurs the extra work of calculating a new expiration time every time the consumer goes to sleep, which isn't a large effort but is certainly greater than that of a simple `if' to see whether a poison pill has been received. Also, there's the annoyance of having the consumer wake up every dT for no good reason, and of delaying the eventual exit by as much as dT after the final producer quits; these annoyances are not huge, but are rattles and pings in an engine that might otherwise run smoothly. Beauty is in the eye of the beholder, circumstances alter cases, and YMMV -- but I find the "poison pill" a clean and unobtrusive way to shut down a producer/consumer system. -- Eric.Sosman@sun.com . 0