It sounds like what you want is not to wait for a thread to finish/exit, but for a thread to
signal that it's done with work, so you can feed them more work.
What I would do is
create 5 work queues, and 1 work result queue
The 5 threads loops by fetching work from its work queue and posts results back to the same result queue.
The main thread(sending work to the 5 threads) would 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 worker threads (processing some work, posting the result back to the main thread) would do:
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 to implement such a queue are here:
http://asgaard.homelinux.org/svn/threadqueue/