pthreadLearner
pthreadLearner

Reputation: 21

pthread multithreading

This is a design question.

I have 6 threads in total and two FIFO queues. The 6 threads are:

The total number of entries is 500.

My question is: how can I keep those 2 semi-consumers and consumers alive while all 500 entries are routed through queue1 to queue 2 and dequeued? For producer threads, it is easy because I can maintain global count of entries.

So if total_entries = 500, return from the routine (produces threads die). I want to use an effective way instead of some hack.

Environment: Linux, pthread, C/C++.

Upvotes: 2

Views: 523

Answers (1)

Ben Jackson
Ben Jackson

Reputation: 93690

In a typical threaded application where the "500 entries" are unknown I think it would be more common for some overall control process to stop all of the threads when the work was done.

For your specific problem you could add the notion of EOF to your FIFOs so that a reader can distinguish between "nothing available now" and "nothing available ever again". This could be as simple as a sentinal value (perhaps 0 is not a legal value for your threads and could represent this).

Then your producer "closes" its end of the FIFO and when it becomes empty the semi-consumers see EOF and close their end of the next FIFO. Just like with real files you'd need reference counts to know when both producers were done or when both semi-consumers were done.

Upvotes: 3

Related Questions