It looks like you do not want to wait for the end or exit of the stream, but for the stream, the signal that it did with the work, so that you can feed them more work.
What would i do
create 5 work queues and 1 work results queue
5 cycles of threads, getting work from the work queue, and messages return to the same result queue.
The main thread (sending work to 5 threads) will do something like this:
for(;;) { struct threadmessage msg; struct *work_result; struct *work; thread_queue_get(&result_queue,NULL,&msg); work_result = msg->data; handle_result(work_result); work = get_more_work(); thread_queue_add(worK_result->queue,work,0); free_work_result(work_result); }
Each of the 5 workflows (processing some work, sending the result back to the main thread) will perform:
for(;;) { struct threadmessage msg; struct *work_result; struct *work; thread_queue_get(my_queue,NULL,&msg); work = msg->data; process(work_result); work_result->queue = my_queue; thread_queue_add(&result_queue,work_result,0); free_work(work); }
The code for implementing such a queue is here: http://asgaard.homelinux.org/svn/threadqueue/
source share