thread poll

转自:http://blog.csdn.net/hinyunsin/article/details/6650879/

定义

         什么是线程池?简单点说,线程池就是有一堆已经创建好了的线程,初始它们都处于空闲等待状态,当有新的任务需要处理的时候,就从这个池子里面取一个空闲等待的线程来处理该任务,当处理完成了就再次把该线程放回池中,以供后面的任务使用。当池子里的线程全都处理忙碌状态时,线程池中没有可用的空闲等待线程,此时,根据需要选择创建一个新的线程并置入池中,或者通知任务线程池忙,稍后再试。

 

为什么要用线程池?

         我们说,线程的创建和销毁比之进程的创建和销毁是轻量级的,但是当我们的任务需要大量进行大量线程的创建和销毁操作时,这个消耗就会变成的相当大。比如,当你设计一个压力性能测试框架的时候,需要连续产生大量的并发操作,这个是时候,线程池就可以很好的帮上你的忙。线程池的好处就在于线程复用,一个任务处理完成后,当前线程可以直接处理下一个任务,而不是销毁后再创建,非常适用于连续产生大量并发任务的场合。

 

线程池工作原理

         线程池中每一个线程的工作过程如下:


图 1: 线程的工作流程

         线程池的任务就在于负责这些线程的创建,销毁和任务处理参数传递、唤醒和等待。

1.      创建若干线程,置入线程池

2.      任务达到时,从线程池取空闲线程

3.      取得了空闲线程,立即进行任务处理

4.      否则新建一个线程,并置入线程池,执行3

5.      如果创建失败或者线程池已满,根据设计策略选择返回错误或将任务置入处理队列,等待处理

6.      销毁线程池

 

图 2:线程池的工作原理

 

线程池设计


数据结构设计


任务设计

[cpp]  view plain copy
  1. typedef struct tp_work_desc_s TpWorkDesc;  
  2. typedef void (*process_job)(TpWorkDesc*job);  
  3. struct tp_work_desc_s {  
  4.          void *ret; //call in, that is arguments  
  5.          void *arg; //call out, that is return value  
  6. };  

其中,TpWorkDesc是任务参数描述,arg是传递给任务的参数,ret则是任务处理完成后的返回值;

process_job函数是任务处理函数原型,每个任务处理函数都应该这样定义,然后将它作为参数传给线程池处理,线程池将会选择一个空闲线程通过调用该函数来进行任务处理;

 

线程设计

[cpp]  view plain copy
  1. typedef struct tp_thread_info_s TpThreadInfo;  
  2. struct tp_thread_info_s {  
  3.          pthread_t thread_id; //thread id num  
  4.          TPBOOL is_busy; //thread status:true-busy;flase-idle  
  5.          pthread_cond_t thread_cond;  
  6.          pthread_mutex_t thread_lock;  
  7.          process_job proc_fun;  
  8.          TpWorkDesc* th_job;  
  9.          TpThreadPool* tp_pool;  
  10. };  

TpThreadInfo是对一个线程的描述。

thread_id是该线程的ID;

is_busy用于标识该线程是否正处理忙碌状态;

thread_cond用于任务处理时的唤醒和等待;

thread_lock,用于任务加锁,用于条件变量等待加锁;

proc_fun是当前任务的回调函数地址;

th_job是任务的参数信息;

tp_pool是所在线程池的指针;

 

线程池设计

[cpp]  view plain copy
  1. typedef struct tp_thread_pool_s TpThreadPool;  
  2. struct tp_thread_pool_s {  
  3.          unsigned min_th_num; //min thread number in the pool  
  4.          unsigned cur_th_num; //current thread number in the pool  
  5.          unsigned max_th_num; //max thread number in the pool  
  6.          pthread_mutex_t tp_lock;  
  7.          pthread_t manage_thread_id; //manage thread id num  
  8.          TpThreadInfo* thread_info;  
  9.          Queue idle_q;  
  10.          TPBOOL stop_flag;  
  11. };  

TpThreadPool是对线程池的描述。

min_th_num是线程池中至少存在的线程数,线程池初始化的过程中会创建min_th_num数量的线程;

cur_th_num是线程池当前存在的线程数量;

max_th_num则是线程池最多可以存在的线程数量;

tp_lock用于线程池管理时的互斥;

manage_thread_id是线程池的管理线程ID;

thread_info则是指向线程池数据,这里使用一个数组来存储线程池中线程的信息,该数组的大小为max_th_num;

idle_q是存储线程池空闲线程指针的队列,用于从线程池快速取得空闲线程;

stop_flag用于线程池的销毁,当stop_flag为FALSE时,表明当前线程池需要销毁,所有忙碌线程在处理完当前任务后会退出;


算法设计


线程池的创建和初始化

线程创建

创建伊始,线程池线程容量大小上限为max_th_num,初始容量为min_th_num;

[cpp]  view plain copy
  1. TpThreadPool *tp_create(unsigned min_num, unsigned max_num) {  
  2.     TpThreadPool *pTp;  
  3.     pTp = (TpThreadPool*) malloc(sizeof(TpThreadPool));  
  4.   
  5.     memset(pTp, 0, sizeof(TpThreadPool));  
  6.   
  7.     //init member var  
  8.     pTp->min_th_num = min_num;  
  9.     pTp->cur_th_num = min_num;  
  10.     pTp->max_th_num = max_num;  
  11.     pthread_mutex_init(&pTp->tp_lock, NULL);  
  12.   
  13.     //malloc mem for num thread info struct  
  14.     if (NULL != pTp->thread_info)  
  15.         free(pTp->thread_info);  
  16.     pTp->thread_info = (TpThreadInfo*) malloc(sizeof(TpThreadInfo) * pTp->max_th_num);  
  17.     memset(pTp->thread_info, 0, sizeof(TpThreadInfo) * pTp->max_th_num);  
  18.   
  19.     return pTp;  
  20. }  

线程初始化

[cpp]  view plain copy
  1. TPBOOL tp_init(TpThreadPool *pTp) {  
  2.     int i;  
  3.     int err;  
  4.     TpThreadInfo *pThi;  
  5.   
  6.     initQueue(&pTp->idle_q);  
  7.     pTp->stop_flag = FALSE;  
  8.   
  9.     //create work thread and init work thread info  
  10.     for (i = 0; i < pTp->min_th_num; i++) {  
  11.         pThi = pTp->thread_info +i;  
  12.         pThi->tp_pool = pTp;  
  13.         pThi->is_busy = FALSE;  
  14.         pthread_cond_init(&pThi->thread_cond, NULL);  
  15.         pthread_mutex_init(&pThi->thread_lock, NULL);  
  16.         pThi->proc_fun = def_proc_fun;  
  17.         pThi->th_job = NULL;  
  18.         enQueue(&pTp->idle_q, pThi);  
  19.   
  20.         err = pthread_create(&pThi->thread_id, NULL, tp_work_thread, pThi);  
  21.         if (0 != err) {  
  22.             perror("tp_init: create work thread failed.");  
  23.             clearQueue(&pTp->idle_q);  
  24.             return FALSE;  
  25.         }  
  26.     }  
  27.   
  28.     //create manage thread  
  29.     err = pthread_create(&pTp->manage_thread_id, NULL, tp_manage_thread, pTp);  
  30.     if (0 != err) {  
  31.         clearQueue(&pTp->idle_q);  
  32.         printf("tp_init: creat manage thread failed\n");  
  33.         return FALSE;  
  34.     }  
  35.   
  36.     return TRUE;  
  37. }  


初始线程池中线程数量为min_th_num,对这些线程一一进行初始化;

将这些初始化的空闲线程一一置入空闲队列;

创建管理线程,用于监控线程池的状态,并适当回收多余的线程资源;

 

线程池的关闭和销毁

[cpp]  view plain copy
  1. void tp_close(TpThreadPool *pTp, TPBOOL wait) {  
  2.     unsigned i;  
  3.   
  4.     pTp->stop_flag = TRUE;  
  5.     if (wait) {  
  6.         for (i = 0; i < pTp->cur_th_num; i++) {  
  7.             pthread_cond_signal(&pTp->thread_info[i].thread_cond);  
  8.         }  
  9.         for (i = 0; i < pTp->cur_th_num; i++) {  
  10.             pthread_join(pTp->thread_info[i].thread_id, NULL);  
  11.             pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);  
  12.             pthread_cond_destroy(&pTp->thread_info[i].thread_cond);  
  13.         }  
  14.     } else {  
  15.         //close work thread  
  16.         for (i = 0; i < pTp->cur_th_num; i++) {  
  17.             kill((pid_t)pTp->thread_info[i].thread_id, SIGKILL);  
  18.             pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);  
  19.             pthread_cond_destroy(&pTp->thread_info[i].thread_cond);  
  20.         }  
  21.     }  
  22.     //close manage thread  
  23.     kill((pid_t)pTp->manage_thread_id, SIGKILL);  
  24.     pthread_mutex_destroy(&pTp->tp_lock);  
  25.   
  26.     //free thread struct  
  27.     free(pTp->thread_info);  
  28.     pTp->thread_info = NULL;  
  29. }  

线程池关闭的过程中,可以选择是否对正在处理的任务进行等待,如果是,则会唤醒所有任务,然后等待所有任务执行完成,然后返回;如果不是,则将立即杀死所有线程,然后返回, 注意:这可能会导致任务的处理中断而产生错误!

 

任务处理

[cpp]  view plain copy
  1. TPBOOL tp_process_job(TpThreadPool *pTp, process_job proc_fun, TpWorkDesc *job) {  
  2.     TpThreadInfo *pThi ;  
  3.     //fill pTp->thread_info's relative work key  
  4.     pthread_mutex_lock(&pTp->tp_lock);  
  5.     pThi = (TpThreadInfo *) deQueue(&pTp->idle_q);  
  6.     pthread_mutex_unlock(&pTp->tp_lock);  
  7.     if(pThi){  
  8.         pThi->is_busy =TRUE;  
  9.         pThi->proc_fun = proc_fun;  
  10.         pThi->th_job = job;  
  11.         pthread_cond_signal(&pThi->thread_cond);  
  12.         DEBUG("Fetch a thread from pool.\n");  
  13.         return TRUE;  
  14.     }  
  15.     //if all current thread are busy, new thread is created here  
  16.     pthread_mutex_lock(&pTp->tp_lock);  
  17.     pThi = tp_add_thread(pTp);  
  18.     pthread_mutex_unlock(&pTp->tp_lock);  
  19.   
  20.     if(!pThi){  
  21.         DEBUG("The thread pool is full, no more thread available.\n");  
  22.         return FALSE;  
  23.     }  
  24.     DEBUG("No more idle thread, created a new one.\n");  
  25.     pThi->proc_fun = proc_fun;  
  26.     pThi->th_job = job;  
  27.   
  28.     //send cond to work thread  
  29.     pthread_cond_signal(&pThi->thread_cond);  
  30.     return TRUE;  
  31. }  

当一个新任务到达是,线程池首先会检查是否有可用的空闲线程,如果是,则采用才空闲线程进行任务处理并返回TRUE,如果不是,则尝试新建一个线程,并使用该线程对任务进行处理,如果失败则返回FALSE,说明线程池忙碌或者出错。

[cpp]  view plain copy
  1. static void *tp_work_thread(void *arg) {  
  2.     pthread_t curid;//current thread id  
  3.     TpThreadInfo *pTinfo = (TpThreadInfo *) arg;  
  4.   
  5.     //wait cond for processing real job.  
  6.     while (!(pTinfo->tp_pool->stop_flag)) {  
  7.         pthread_mutex_lock(&pTinfo->thread_lock);  
  8.         pthread_cond_wait(&pTinfo->thread_cond, &pTinfo->thread_lock);  
  9.         pthread_mutex_unlock(&pTinfo->thread_lock);  
  10.   
  11.         //process  
  12.         pTinfo->proc_fun(pTinfo->th_job);  
  13.   
  14.         //thread state be set idle after work  
  15.         //pthread_mutex_lock(&pTinfo->thread_lock);  
  16.         pTinfo->is_busy = FALSE;  
  17.         enQueue(&pTinfo->tp_pool->idle_q, pTinfo);  
  18.         //pthread_mutex_unlock(&pTinfo->thread_lock);  
  19.         DEBUG("Job done, I am idle now.\n");  
  20.     }  
  21. }  

上面这个函数是任务处理函数,该函数将始终处理等待唤醒状态,直到新任务到达或者线程销毁时被唤醒,然后调用任务处理回调函数对任务进行处理;当任务处理完成时,则将自己置入空闲队列中,以供下一个任务处理。

[cpp]  view plain copy
  1. TpThreadInfo *tp_add_thread(TpThreadPool *pTp) {  
  2.     int err;  
  3.     TpThreadInfo *new_thread;  
  4.   
  5.     if (pTp->max_th_num <= pTp->cur_th_num)  
  6.         return NULL;  
  7.   
  8.     //malloc new thread info struct  
  9.     new_thread = pTp->thread_info + pTp->cur_th_num;   
  10.   
  11.     new_thread->tp_pool = pTp;  
  12.     //init new thread's cond & mutex  
  13.     pthread_cond_init(&new_thread->thread_cond, NULL);  
  14.     pthread_mutex_init(&new_thread->thread_lock, NULL);  
  15.   
  16.     //init status is busy, only new process job will call this function  
  17.     new_thread->is_busy = TRUE;  
  18.     err = pthread_create(&new_thread->thread_id, NULL, tp_work_thread, new_thread);  
  19.     if (0 != err) {  
  20.         free(new_thread);  
  21.         return NULL;  
  22.     }  
  23.     //add current thread number in the pool.  
  24.     pTp->cur_th_num++;  
  25.   
  26.     return new_thread;  
  27. }  

上面这个函数用于向线程池中添加新的线程,该函数将会在当线程池没有空闲线程可用时被调用。

函数将会新建一个线程,并设置自己的状态为busy(立即就要被用于执行任务)。

线程池管理

线程池的管理主要是监控线程池的整体忙碌状态,当线程池大部分线程处于空闲状态时,管理线程将适当的销毁一定数量的空闲线程,以便减少线程池对系统资源的消耗。

 

这里设计认为,当空闲线程的数量超过线程池线程数量的1/2时,线程池总体处理空闲状态,可以适当销毁部分线程池的线程,以减少线程池对系统资源的开销。

 

线程池状态计算

这里的BUSY_THRESHOLD的值是0.5,也即是当空闲线程数量超过一半时,返回0,说明线程池整体状态为闲,否则返回1,说明为忙。

[cpp]  view plain copy
  1. int tp_get_tp_status(TpThreadPool *pTp) {  
  2.     float busy_num = 0.0;  
  3.     int i;  
  4.   
  5.     //get busy thread number  
  6.     busy_num = pTp->cur_th_num - pTp->idle_q.count;     
  7.   
  8.     DEBUG("Current thread pool status, current num: %u, busy num: %u, idle num: %u\n", pTp->cur_th_num, (unsigned)busy_num, pTp->idle_q.count);  
  9.     //0.2? or other num?  
  10.     if (busy_num / (pTp->cur_th_num) < BUSY_THRESHOLD)  
  11.         return 0;//idle status  
  12.     else  
  13.         return 1;//busy or normal status      
  14. }  

线程的销毁算法

1.      从空闲队列中dequeue一个空闲线程指针,该指针指向线程信息数组的某项,例如这里是p;

2.      销毁该线程

3.      把线程信息数组的最后一项拷贝至位置p

4.      线程池数量减少一,即cur_th_num--


图 3:线程销毁

 

[cpp]  view plain copy
  1. TPBOOL tp_delete_thread(TpThreadPool *pTp) {  
  2.     unsigned idx;  
  3.     TpThreadInfo *pThi;  
  4.     TpThreadInfo tT;  
  5.   
  6.     //current thread num can't < min thread num  
  7.     if (pTp->cur_th_num <= pTp->min_th_num)  
  8.         return FALSE;  
  9.     //pthread_mutex_lock(&pTp->tp_lock);  
  10.     pThi = deQueue(&pTp->idle_q);  
  11.     //pthread_mutex_unlock(&pTp->tp_lock);  
  12.     if(!pThi)  
  13.       return FALSE;  
  14.       
  15.     //after deleting idle thread, current thread num -1  
  16.     pTp->cur_th_num--;  
  17.     memcpy(&tT, pThi, sizeof(TpThreadInfo));  
  18.     memcpy(pThi, pTp->thread_info + pTp->cur_th_num, sizeof(TpThreadInfo));  
  19.   
  20.     //kill the idle thread and free info struct  
  21.     kill((pid_t)tT.thread_id, SIGKILL);  
  22.     pthread_mutex_destroy(&tT.thread_lock);  
  23.     pthread_cond_destroy(&tT.thread_cond);  
  24.   
  25.     return TRUE;  
  26. }  

线程池监控

线程池通过一个管理线程来进行监控,管理线程将会每隔一段时间对线程池的状态进行计算,根据线程池的状态适当的销毁部分线程,减少对系统资源的消耗。

 

[cpp]  view plain copy
  1. static void *tp_manage_thread(void *arg) {  
  2.     TpThreadPool *pTp = (TpThreadPool*) arg;//main thread pool struct instance  
  3.   
  4.     //1?  
  5.     sleep(MANAGE_INTERVAL);  
  6.   
  7.     do {  
  8.         if (tp_get_tp_status(pTp) == 0) {  
  9.             do {  
  10.                 if (!tp_delete_thread(pTp))  
  11.                     break;  
  12.             } while (TRUE);  
  13.         }//end for if  
  14.   
  15.         //1?  
  16.         sleep(MANAGE_INTERVAL);  
  17.     } while (!pTp->stop_flag);  
  18.     return NULL;  
  19. }  

程序测试

至此,我们的设计需要使用一个测试程序来进行验证。于是,我们写下这样一段代码。

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include "thread_pool.h"  
  4.   
  5. #define THD_NUM 10   
  6. void proc_fun(TpWorkDesc *job){  
  7.     int i;  
  8.     int idx=*(int *)job->arg;  
  9.     printf("Begin: thread %d\n", idx);  
  10.     sleep(3);  
  11.     printf("End:   thread %d\n", idx);  
  12. }  
  13.   
  14. int main(int argc, char **argv){  
  15.     TpThreadPool *pTp= tp_create(5,10);  
  16.     TpWorkDesc pWd[THD_NUM];  
  17.     int i, *idx;  
  18.   
  19.     tp_init(pTp);  
  20.     for(i=0; i < THD_NUM; i++){  
  21.         idx=(int *) malloc(sizeof(int));  
  22.         *idx=i;  
  23.         pWd[i].arg=idx;  
  24.         tp_process_job(pTp, proc_fun, pWd+i);  
  25.         usleep(400000);  
  26.     }  
  27.     //sleep(1);  
  28.     tp_close(pTp, TRUE);  
  29.     free(pTp);  
  30.     printf("All jobs done!\n");  
  31.     return 0;  
  32. }  

执行结果:

 

源码下载

地址:https://sourceforge.net/projects/thd-pool-linux/

备注

该线程池设计比较简单,尚存在不少BUG,欢迎各位提出改进意见。

 


修正:

2011/08/04:

tp_close函数增加队列清空操作,参见源码注释部分。

[cpp]  view plain copy
  1. void tp_close(TpThreadPool *pTp, TPBOOL wait) {  
  2.     unsigned i;  
  3.   
  4.   
  5.     pTp->stop_flag = TRUE;  
  6.     if (wait) {  
  7.         for (i = 0; i < pTp->cur_th_num; i++) {  
  8.             pthread_cond_signal(&pTp->thread_info[i].thread_cond);  
  9.         }  
  10.         for (i = 0; i < pTp->cur_th_num; i++) {  
  11.             pthread_join(pTp->thread_info[i].thread_id, NULL);  
  12.             pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);  
  13.             pthread_cond_destroy(&pTp->thread_info[i].thread_cond);  
  14.         }  
  15.     } else {  
  16.         //close work thread  
  17.         for (i = 0; i < pTp->cur_th_num; i++) {  
  18.             kill((pid_t)pTp->thread_info[i].thread_id, SIGKILL);  
  19.             pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);  
  20.             pthread_cond_destroy(&pTp->thread_info[i].thread_cond);  
  21.         }  
  22.     }  
  23.     //close manage thread  
  24.     kill((pid_t)pTp->manage_thread_id, SIGKILL);  
  25.     pthread_mutex_destroy(&pTp->tp_lock);  
  26.   
  27.   
  28.     clearQueue(&pTp->idle_q); /** 这里添加队列清空 **/  
  29.     //free thread struct  
  30.     free(pTp->thread_info);  
  31.     pTp->thread_info = NULL;  
  32. }  


上述操作将导致段错误,原因是队列在删除元素的时候,对元素进行了free。而我们的元素其实是数组中某个元素的地址,这里将导致段错误的发生。源码中队列部分增加了元素释放函数回调,设置该函数为NULL或者空函数(什么都不做),在删除元素时将不会进行free操作。完整源码请到上面的地址下载。


在线程池初始化时,需要设置元素释放函数为NULL,参见源码注释部分。

[cpp]  view plain copy
  1. TPBOOL tp_init(TpThreadPool *pTp) {  
  2.     int i;  
  3.     int err;  
  4.     TpThreadInfo *pThi;  
  5.   
  6.     initQueue(&pTp->idle_q, NULL); /** 初始化时设置元素释放函数为NULL **/  
  7.     pTp->stop_flag = FALSE;  
  8.   
  9.     //create work thread and init work thread info  
  10.     for (i = 0; i < pTp->min_th_num; i++) {  
  11.         pThi = pTp->thread_info +i;  
  12.         pThi->tp_pool = pTp;  
  13.         pThi->is_busy = FALSE;  
  14.         pthread_cond_init(&pThi->thread_cond, NULL);  
  15.         pthread_mutex_init(&pThi->thread_lock, NULL);  
  16.         pThi->proc_fun = def_proc_fun;  
  17.         pThi->th_job = NULL;  
  18.         enQueue(&pTp->idle_q, pThi);  
  19.   
  20.         err = pthread_create(&pThi->thread_id, NULL, tp_work_thread, pThi);  
  21.         if (0 != err) {  
  22.             perror("tp_init: create work thread failed.");  
  23.             clearQueue(&pTp->idle_q);  
  24.             return FALSE;  
  25.         }  
  26.     }  
  27.   
  28.     //create manage thread  
  29.     err = pthread_create(&pTp->manage_thread_id, NULL, tp_manage_thread, pTp);  
  30.     if (0 != err) {  
  31.         clearQueue(&pTp->idle_q);  
  32.         printf("tp_init: creat manage thread failed\n");  
  33.         return FALSE;  
  34.     }  
  35.   
  36.     return TRUE;  
  37. }  


这里顺便附上队列头文件部分源码:

[cpp]  view plain copy
  1. #ifndef __QUEUE_H_  
  2. #define __QUEUE_H_  
  3.   
  4. #include <pthread.h>  
  5.   
  6. typedef struct sNode QNode;  
  7. typedef struct queueLK Queue;  
  8.   
  9. typedef void * EType;  
  10.   
  11. typedef void (*free_data_fun)(void *data);  
  12.   
  13. struct sNode {  
  14.     EType * data;  
  15.     struct sNode *next;  
  16. };  
  17.   
  18. struct queueLK {  
  19.     struct sNode *front;  
  20.     struct sNode *rear;  
  21.     free_data_fun free_fun;  
  22.     unsigned count;  
  23.     pthread_mutex_t lock;  
  24. };  
  25.   
  26. void initQueue(Queue *hq, free_data_fun pff);  
  27. int enQueue(Queue *hq, EType x);  
  28. EType deQueue(Queue *hq);  
  29. EType peekQueue(Queue *hq);  
  30. int isEmptyQueue(Queue *hq);  

  1. void clearQueue(Queue *hq);  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值