Why in some implementation of the thread pool the producer and consumer models are not used

I intend to implement a thread pool for thread management in my project. The main structure of the thread pool in my queue is the queue, and some threads create tasks in this queue, and some threads managed by the thread pool wait for this task to complete. I think this is a problem for producers and consumers. But when I use the google thread pool implementation on the Internet, I find that this implementation rarely uses this classic model, so my question is why they don’t use this classic model, does this model have any flaws? why don't they use full semaphore and empty semaphore for synchronization?

+4
source share
2 answers

If you have multiple threads waiting on the same resource (in this case, semaphores and a queue), you create a bottleneck. You set all tasks in one queue, even if you have several employees. Logically, this may make sense if workers are usually idle, but the whole point of the thread pool is to deal with a heavily loaded scenario where workers remain busy (for maximum end-to-end input). Using one input queue will be especially bad in a multiprocessor system where all workers read and write the queue head when they try to get the next task. Although the lock conflict may be low, the queue head pointer will still need to be exchanged / transferred from one processor cache to another each time it is updated.

Think about the ideal case: all workers are always busy. When a new task is in the queue, you want it to be sent to the employee who will perform the current / pending tasks first.

If you had a non-partisan oracle as a client who could tell you which worker should queue a new task, and each employee had his own turn, then you could implement each employee with his own multitasking scenario, a queue with one reader and always send new tasks in the best turn, thereby eliminating worker conflicts in one common input queue. Of course, you don’t have such an oracle, but this mechanism still works very well until the employee has completed the tasks or the queues are balanced. “Job theft” refers to these cases, but at the same time reduces competition compared to a single-queue situation.

See also: Is Stealing always the most suitable user-level thread scheduling algorithm?

+7
source

Why there is no implementation of the producer and consumer

This model is very general and can have many different explanations; one of the implementations may be Queue :

Try Apache APR Queue:

It is documented as Thread Safe FIFO bounded queue .

http://apr.apache.org/docs/apr-util/1.3/apr__queue_8h.html

0
source

Source: https://habr.com/ru/post/1496479/


All Articles