Implementation of a simple thread pool

       A thread pool is a form of multi-threaded processing in which tasks are added to a queue during processing, and then threads are created to start and process these tasks.

component

Server programs can use threading technology to respond to client requests.

    1. Thread pool manager: used to create and manage threads

    2. Worker thread: The thread that is processing the task

    3. Task interface: an excuse that each task must implement for the execution of tasks scheduled by worker threads

    4. Task queue: used to store tasks that have not been processed. Provides a caching mechanism.

Implementation of thread pool
  • Used to perform a large number of relatively short-lived tasks
  • When tasks increase, the number of threads in the thread pool can be dynamically increased until a threshold is reached
  • When the task is completed, the threads in the thread pool can be dynamically destroyed
  • The implementation of the thread pool is essentially an application of the producer-consumer model. The producer thread adds tasks to the task queue. Once a task arrives in the queue, if there is a waiting thread, it wakes up to execute the task. If there is no waiting thread and the number of threads does not reach the threshold, a new thread is created to execute the task.

1. The following is the composition of the task structure:

// Task structure, put the task into the queue to be executed by the thread in the thread pool
typedef struct task {
    void *(*run)(void *arg); //Task callback function
    void *arg; //callback function parameters
    struct  task  *next;
}task_t;

2. The following is the composition of the thread pool structure:

// thread pool structure
typedef struct threadpool {
    condition_t ready; //task ready or thread pool destruction notification
    task_t *first; //task queue head pointer
    task_t *last; //task queue tail pointer
    int count; //The current number of threads in the thread pool
    int idle; //Number of threads currently waiting for tasks in the thread pool
    int max_threads; //The maximum number of threads allowed in the thread pool
    int quit; //Set to 1 when destroying the thread pool
}threadpool_t;
3. Interface functions provided by the thread pool
void threadpool_init (threadpool_t *pool, int threads); //Initialize the thread pool
void threadpool_add_task (threadpool_t *pool, void *(*run) (void *arg), void *arg); //Add a task to the thread pool
void threadpool_destroy(threadpool_t *pool); //Destroy the thread pool

Guess you like

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