Design a thread pool

  Many server applications, such as Web servers, database servers, file servers, or mail servers, are geared toward handling a large number of short tasks from some remote source. The request arrives at the server in some way, perhaps through a network protocol (such as HTTP, FTP, or POP), through a JMS queue, or perhaps by polling a database. Regardless of how the request arrives, it is often the case in server applications that a single task takes a very short time to process and the number of requests is huge.
  Thread pool technology is a "resource pooling" technology. The idea of ​​this technology is to use existing threads to serve tasks that need to be processed.
  When to use thread pool technology? The number of tasks processed by the server is large, and the processing time of a single task is relatively short. In this case, thread pool technology is suitable. Because if the thread pool technology is not used, the server needs to create a thread for each short task and destroy the thread after the task is completed. Creating/destroying threads frequently and frequently will consume a lot of system resources and reduce server performance.
  Generally a simple thread pool contains at least the following components.
    (1) Thread Pool Manager (ThreadPoolManager): used to create and manage thread pools
    (2) Work thread (WorkThread): threads in the thread pool
    (3) Task interface (Task): the interface that each task must implement for Worker threads schedule the execution of tasks.
    (4) Task queue: used to store unprocessed tasks. Provides a buffer mechanism.

  An example of a thread pool (code source: https://github.com/Pithikos/C-Thread-Pool )

/* Binary semaphore */
typedef struct bsem {
    pthread_mutex_t mutex;//互斥变量
    pthread_cond_t   cond;//条件变量
    int v; //v的值只有0,1两种
} bsem;


/* Job */ /* 任务接口 */
typedef struct job{
    struct job*  prev;                   /* pointer to previous job   */
    void   (*function)(void* arg);       /* function pointer          */ /* 指向任务需要执行的函数 */
    void*  arg;                          /* function's argument       */ /* 上述函数的参数。如果需要传递多个参数,那么arg应该是结构体指针 */
} job;


/* Job queue */ /* 任务队列 */
typedef struct jobqueue{
    pthread_mutex_t rwmutex;             /* used for queue r/w access */ /* 同步队列的读写操作,避免同时操作读和写 */
    job  *front;                         /* pointer to front of queue */
    job  *rear;                          /* pointer to rear  of queue */
    bsem *has_jobs;                      /* flag as binary semaphore  */
    int   len;                           /* number of jobs in queue   */
} jobqueue;


/* Thread */ /* 工作线程 */
typedef struct thread{
    int       id;                        /* friendly id               */ /* 标识第几个线程 */
    pthread_t pthread;                   /* pointer to actual thread  */ /* 真正的线程ID */
    struct thpool_* thpool_p;            /* access to thpool          */ /* 指向该线程所在的线程池 */
} thread;


/* Threadpool */ /* 线程池管理器 */
typedef struct thpool_{
    thread**   threads;                  /* pointer to threads        */ /* 指向存放线程的数组 */
    volatile int num_threads_alive;      /* threads currently alive   */ /* 线程池中线程总数 */
    volatile int num_threads_working;    /* threads currently working */ /* 当前正在工作的线程数目 */
    pthread_mutex_t  thcount_lock;       /* used for thread count etc */ /* 线程池操作的互斥锁 */
    pthread_cond_t  threads_all_idle;    /* signal to thpool_wait     */ /* 等待所有任务均完成的条件变量 */
    jobqueue  jobqueue;                  /* job queue                 */ /* 指向任务队列 */
} thpool_;

  
  
  

Guess you like

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