Understanding of thread pools

Understanding of thread pools:

    Imagine a medical examination scene, 100 classmates (s) are in room A, 3 doctors (d) are in room B (asleep), and a teacher who organizes students' medical examinations (asleep). The administrator looked at the situation in room B, woke up the teacher and said: The three doctors are free and can call someone in. The teacher checked that d1, d2, and d3 were idle, so the teacher called s1 to prepare, and after seeing that s1 was ready, the administrator asked the administrator to wake up d1, and d1 started the physical examination for s1; the teacher then called s2 to prepare, and saw that s2 was ready After that, ask the administrator to wake up d2, and d2 starts the physical examination for s2; the teacher calls s3 to prepare again. After seeing that s3 is ready, ask the administrator to wake up d3, and d3 starts the physical examination for s3. After arranging 3 students, the teacher started to sleep after seeing that there was nothing to do for the time being, and asked the administrator to help him and wake him up if he said there was a doctor free. After a while, d2 finished the inspection first, and d2 said to the administrator: I have finished the inspection, please help me inform the teacher that 1 student can come in now, I will go to bed first, if the teacher calls me, you will wake me up. The administrator woke up the teacher and said that 1 student can come in. The teacher took a look and found that d2 had been checked, so he notified the next student that s4 was going to go to d2. When he saw that s4 was ready, he asked the administrator to call d2. After waking up d2, d2 started the physical examination for s4. , the teacher started to sleep again and asked the administrator to help him watch and wake up the doctor if he was free.

    In this example, 3 doctors, 1 teacher, and 1 administrator represent 5 threads respectively, and doing physical examinations for 100 students represents a multi-threaded task. The administrator here refers to the thread maintained by the system (personal understanding), which is uniformly maintained by the system when it involves delay and waiting, such as waiting for a mutex or sleep, so that waiting should not consume additional CPU resources. The teacher corresponds to the main thread of the program and is responsible for the distribution of tasks, but the main thread does not need to run all the time, because the thread pool does not necessarily have idle threads, then the main thread needs to wait to see if there are idle threads, and if you use while (isbusy) and other methods It takes CPU to judge, so it is a good practice to hand over the waiting work to the mutex or condition variable. The mutex is waited by the system thread and does not need to consume additional CPU, that is, the work done by the administrator. The 3 doctors correspond to a thread pool with 3 threads. The threads in the thread pool will also involve waiting for task distribution, and the waiting work should also be handed over to the mutex.

    This example is just a relatively simple scenario. Suppose everyone has only one common medical examination result form. Once the physical examination is completed, one person will fill it out immediately. Then what if several doctors have completed the physical examination at the same time? Here again comes the question of waiting. To be continued...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324796608&siteId=291194637