So there are function pointers and void pointers in the struct. A task needs to be specified: the corresponding function to be run and the parameters of the function. Used to save a task waiting to be executed. There are mainly two custom data structures threadpool_task_t Project Home Page: threadpool data structure The author says that more features will be added later, so we can use this version as a guide after learning. Here is a simple implementation of thread pool, which pre-derives a specified number of threads at the time of creation, and then goes to the task queue to fetch the added tasks for processing. They are in libpthrea, therefore, we should add -lpthread to link our program. In GNU/Linux, the pthread functions are not included in the standard C library. This benefit is very significant when the task processing time is relatively short, and it can improve the efficiency of task processing. Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header file. One advantage of thread pooling is that it reduces the time of thread creation and destruction. Threads constantly take requests out of the request queue and continue to process the next request after completion. So what is the task? In the background server, for example, every user's request is a task. If the administrator is smarter, he may reduce the number of threads when there are no tasks or few tasks, and increase the number of threads when tasks can not be handled later, thus realizing the dynamic management of resources. If there are no tasks, the thread will sleep until the task queue is not empty.
After processing, it goes on to the queue to fetch the task. If the queue is not empty, it takes a task to process at the head of the queue (each task can only be taken by one thread). So what does the administrator do? A simple way is for an administrator to manage a task queue and add tasks to the end of the queue if new tasks are received. What is a thread pool? My simple understanding is that there is a set of pre-derived threads, and then an administrator to manage and schedule these threads, you just need to constantly give him the task to complete, he will schedule the resources of the thread to help you complete.
If not, it is better to understand it here: POSIX multithreading foundation. It is assumed that the reader has a certain multithreading foundation. Thread pool can be said to be a component that is often used in projects. Threadpool-a simple thread pool based on pthread Introduction to Thread Pool