Subj : Re: waiting on signals from multiple threads using pthreads To : comp.programming.threads From : Gianni Mariani Date : Sun Mar 13 2005 07:41 am red0356@yahoo.com wrote: > In our application, we have three (possibly more) worker threads > performing some task and another thread which waits for completion of > all three tasks from the worker threads. All three worker threads are > NOT synchronous and it is possible to have one worker thread complete > multiple tasks before another worker thread completes one task. The > waiting thread must wait for all three worker threads to complete at > least one task, before doing its job. What is the best pthread > implementation for this problem? > > Thanks in advance for all replies. > posix had a condition variable (combined with a mutex). ======= psuedo code (C++-ish) ========== Mutex mutex; Conditional cv( mutex ); void worker() { while ( Work work = GetWork() ) { work.Do(); Lock lock( mutex ); done_queue.insert( work ); cv.Signal(); } } void another() { Lock lock( mutex ); do ( Work work = done_queue.pop(); if ( ! work ) { if ( finished ) return; cv.Wait(); } else { Unlock unlock( mutex ); work.do_another(); } } while ( 1 ); } .