I am curious to find out if there is a widespread solution for managing stream resources in a stream pool considering the following scenarios / limitations:
- Incoming jobs are all of the same nature and can be processed by any thread in the pool.
- Incoming tasks will be "posted" in different queues based on some attribute of the incoming task, so that all tasks moving to the same bucket / queue MUST be processed sequentially.
- Some buckets will be less busy than others at different points during the life of the program.
My question is about threadpool implementation theory. What algorithm can be used to efficiently distribute available threads for incoming tasks in all buckets?
Change Another design goal is to eliminate as much latency as possible between a job in the queue and picking it up for processing, subject to the availability of free threads.
Edit2 . In that case, if I think about the presence of a relatively large number of queues (50-100), which have unpredictable levels of activity, but probably only 25% of them will be active at any given time.
The first (and most expensive) solution that I can think of is to simply have 1 thread assigned to each queue. Although this will ensure that incoming requests are received immediately, it is clearly inefficient.
The second solution is to combine the queues based on the expected activity levels so that the number of queues is built into the number of threads in the pool, allowing one thread to be assigned to each queue. The problem here is that incoming jobs that might otherwise be processed in parallel will have to wait for each other.
The third solution is to create the maximum number of queues, one for each set of tasks that must be processed sequentially, but to distribute threads only by the number of waiting to wait at any given time (which can also be configured by the pool at runtime). Therefore, my question arises here: given that we have more queues than threads, how does the pool go about how to most efficiently distribute thread threads to incoming jobs?
I would like to know if there is a generally accepted approach. Or, if there are different approaches - who uses which? What are the advantages / disadvantages etc.?
Edit3 : this can be best expressed in pseudo-code.
hifier
source share