线程池的设计与测试

来源:互联网 发布:怎样做好网络销售 编辑:程序博客网 时间:2024/06/08 05:54

编写了一个最基本的线程池类,处理用c_work表示的工作任务。C++还很不熟练,欢迎会C++的提出宝贵的修改意见。

程序有注释,所以应该很好读懂。测试程序在下面。




[cpp] view plaincopyprint?
  1. ///////////////////////////////////////////////////////  
  2. //线程池类   
  3. ///////////////////////////////////////////////////////  
  4. #include <pthread.h>  
  5. #include <stdlib.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <assert.h>  
  9.   
  10. const int DEFAULT_MAX_THREAD_NUM = 10;  
  11. const int MAX_WORK_NUM = 100000;  
  12. //c_worker类  
  13. class c_work  
  14. {  
  15.     public:  
  16.         c_work():process(NULL), arg(NULL), next(NULL){}  
  17.         c_work(void *(*prcss)(void *), void *arg):  
  18.             process(prcss), arg(arg), next(NULL) {}  
  19.         ~c_work();   
  20.   
  21.         void *(*process)(void *);  
  22.         void *arg;  
  23.         unsigned char type; //最高位表示arg是否需要delete操作  
  24.         c_work *next;  
  25. };  
  26.   
  27. c_work::~c_work()  
  28. {  
  29.     unsigned char ifdel = type >> 7;  
  30.     if (ifdel)  
  31.     {  
  32.         delete arg;  
  33.         arg = NULL;  
  34.     }  
  35. }  
  36.   
  37. class c_thread_pool  
  38. {  
  39.     public:  
  40.         c_thread_pool();  
  41.         c_thread_pool(const int max_thread_num);  
  42.         ~c_thread_pool();  
  43.   
  44.         int add_work(c_work work);  
  45.         static void *thread_routine(void *arg);  
  46.   
  47.         pthread_mutex_t queue_lock;  
  48.         pthread_cond_t queue_cond;  
  49.   
  50. //  private:  
  51.         c_work *queue_head;  
  52.         c_work *queue_tail;  
  53.         int shutdown;  
  54.   
  55.         pthread_t *threadid;  
  56.         int max_thread_num;  
  57.         int cur_queue_size;  
  58. };  
  59.   
  60. c_thread_pool::c_thread_pool()  
  61. {  
  62.   
  63.     pthread_mutex_init(&queue_lock, NULL);  
  64.     pthread_cond_init(&queue_cond, NULL);  
  65.   
  66.     //工作队列初始化  
  67.     queue_head = NULL;  
  68.     queue_tail = NULL;  
  69.       
  70.     max_thread_num = max_thread_num;  
  71.     cur_queue_size = 0;  
  72.   
  73.     shutdown = 0;  
  74.   
  75.     max_thread_num = DEFAULT_MAX_THREAD_NUM;  
  76.     threadid = new pthread_t[max_thread_num];  
  77.     int i = 0;  
  78.   
  79.     for (i = 0; i < max_thread_num; i++)  
  80.     {  
  81.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  82.     }  
  83. }  
  84.   
  85.   
  86. c_thread_pool::c_thread_pool(int max_thread_num)  
  87. {  
  88.   
  89.     pthread_mutex_init(&queue_lock, NULL);  
  90.     pthread_cond_init(&queue_cond, NULL);  
  91.   
  92.     //工作队列初始化  
  93.     queue_head = NULL;  
  94.     queue_tail = NULL;  
  95.       
  96.     max_thread_num = max_thread_num;  
  97.     cur_queue_size = 0;  
  98.   
  99.     threadid = new pthread_t[max_thread_num];  
  100.     int i = 0;  
  101.     for (i = 0; i < max_thread_num; i++)  
  102.     {  
  103.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  104.     }  
  105. }  
  106.   
  107. /*向线程池中的任务队列加入任务*/  
  108. int c_thread_pool::add_work(c_work work)  
  109. {  
  110.     c_work *newwork = new c_work;  
  111.     newwork->process = work.process;  
  112.     newwork->arg = work.arg;  
  113.     newwork->next = NULL;  
  114.   
  115.     pthread_mutex_lock(&queue_lock);  
  116.       
  117.     /*将任务加入到等待队列中*/  
  118.     if (queue_head != NULL && queue_tail != NULL)  
  119.     {  
  120.         queue_tail->next = newwork;  
  121.         queue_tail = newwork;     
  122.     }  
  123.     else  
  124.     {  
  125.         //空队列  
  126.         queue_head = newwork;  
  127.         queue_tail = newwork;  
  128.     }  
  129.   
  130.     cur_queue_size++;  
  131.     pthread_mutex_unlock(&queue_lock);  
  132.     /*等待队列中有任务了,唤醒一个等待线程,注意如果所有线程都在忙碌,这句没有任何作用*/  
  133.     pthread_cond_signal(&(queue_cond));   
  134.     printf("add work returned!\n");  
  135.     return 0;  
  136. }  
  137.   
  138.   
  139. void* c_thread_pool::thread_routine(void *arg)  
  140. {  
  141.     c_thread_pool *pool = (c_thread_pool *)arg;  
  142.     int i = 0;  
  143.     while (1)  
  144.     {  
  145.         pthread_mutex_lock(&(pool->queue_lock));  
  146.         //如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意  
  147.          // pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁  
  148.   
  149.         //标注:注意这一如果任务队列不为空的话,while语句将被跳过,直接执行下面的调用。  
  150.         while (pool->cur_queue_size == 0 && pool->shutdown)  
  151.         {  
  152.             pthread_cond_wait(&(pool->queue_cond), &(pool->queue_lock));  
  153.         }  
  154.   
  155.   
  156.         //等待队列长度减去1,并取出链表中的头元素  
  157.         if (pool->cur_queue_size > 0 && pool->queue_head != NULL)  
  158.         {  
  159.             printf("IN THREAD ROUTINE size = %d && queue head is not NULL\n", pool->cur_queue_size);  
  160.             pool->cur_queue_size--;  
  161.             c_work *work = pool->queue_head;  
  162.             pool->queue_head = work->next;  
  163.             pthread_mutex_unlock(&(pool->queue_lock));  
  164.   
  165.             //调用回调函数,执行测试任务  
  166.             //////////////////////////////////////////  
  167.             (*(work->process))(work->arg);  
  168.             free(work);  
  169.             work = NULL;  
  170.         }  
  171.         else //不可达  
  172.         {  
  173.             pthread_mutex_unlock(&(pool->queue_lock));  
  174.         }  
  175.     }  
  176.       
  177. }  
  178.   
  179. c_thread_pool::~c_thread_pool()  
  180. {  
  181.     for (int i = 0; i < max_thread_num; ++i)   
  182.         pthread_cancel(threadid[i]);  
  183.     for (c_work *w_t = queue_head; w_t != NULL;)  
  184.     {  
  185.         c_work *temp = w_t->next;  
  186.         delete w_t;  
  187.         w_t = temp;  
  188.     }  
  189.     delete [] threadid;  
  190. }  



[cpp] view plaincopyprint?
  1. ///////////////////////////////////////////////////////  
  2. //线程池类   
  3. ///////////////////////////////////////////////////////  
  4. #include <pthread.h>  
  5. #include <stdlib.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <assert.h>  
  9.   
  10. const int DEFAULT_MAX_THREAD_NUM = 10;  
  11. const int MAX_WORK_NUM = 100000;  
  12. //c_worker类  
  13. class c_work  
  14. {  
  15.     public:  
  16.         c_work():process(NULL), arg(NULL), next(NULL){}  
  17.         c_work(void *(*prcss)(void *), void *arg):  
  18.             process(prcss), arg(arg), next(NULL) {}  
  19.         ~c_work();   
  20.   
  21.         void *(*process)(void *);  
  22.         void *arg;  
  23.         unsigned char type; //最高位表示arg是否需要delete操作  
  24.         c_work *next;  
  25. };  
  26.   
  27. c_work::~c_work()  
  28. {  
  29.     unsigned char ifdel = type >> 7;  
  30.     if (ifdel)  
  31.     {  
  32.         delete arg;  
  33.         arg = NULL;  
  34.     }  
  35. }  
  36.   
  37. class c_thread_pool  
  38. {  
  39.     public:  
  40.         c_thread_pool();  
  41.         c_thread_pool(const int max_thread_num);  
  42.         ~c_thread_pool();  
  43.   
  44.         int add_work(c_work work);  
  45.         static void *thread_routine(void *arg);  
  46.   
  47.         pthread_mutex_t queue_lock;  
  48.         pthread_cond_t queue_cond;  
  49.   
  50. //  private:  
  51.         c_work *queue_head;  
  52.         c_work *queue_tail;  
  53.         int shutdown;  
  54.   
  55.         pthread_t *threadid;  
  56.         int max_thread_num;  
  57.         int cur_queue_size;  
  58. };  
  59.   
  60. c_thread_pool::c_thread_pool()  
  61. {  
  62.   
  63.     pthread_mutex_init(&queue_lock, NULL);  
  64.     pthread_cond_init(&queue_cond, NULL);  
  65.   
  66.     //工作队列初始化  
  67.     queue_head = NULL;  
  68.     queue_tail = NULL;  
  69.       
  70.     max_thread_num = max_thread_num;  
  71.     cur_queue_size = 0;  
  72.   
  73.     shutdown = 0;  
  74.   
  75.     max_thread_num = DEFAULT_MAX_THREAD_NUM;  
  76.     threadid = new pthread_t[max_thread_num];  
  77.     int i = 0;  
  78.   
  79.     for (i = 0; i < max_thread_num; i++)  
  80.     {  
  81.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  82.     }  
  83. }  
  84.   
  85.   
  86. c_thread_pool::c_thread_pool(int max_thread_num)  
  87. {  
  88.   
  89.     pthread_mutex_init(&queue_lock, NULL);  
  90.     pthread_cond_init(&queue_cond, NULL);  
  91.   
  92.     //工作队列初始化  
  93.     queue_head = NULL;  
  94.     queue_tail = NULL;  
  95.       
  96.     max_thread_num = max_thread_num;  
  97.     cur_queue_size = 0;  
  98.   
  99.     threadid = new pthread_t[max_thread_num];  
  100.     int i = 0;  
  101.     for (i = 0; i < max_thread_num; i++)  
  102.     {  
  103.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  104.     }  
  105. }  
  106.   
  107. /*向线程池中的任务队列加入任务*/  
  108. int c_thread_pool::add_work(c_work work)  
  109. {  
  110.     c_work *newwork = new c_work;  
  111.     newwork->process = work.process;  
  112.     newwork->arg = work.arg;  
  113.     newwork->next = NULL;  
  114.   
  115.     pthread_mutex_lock(&queue_lock);  
  116.       
  117.     /*将任务加入到等待队列中*/  
  118.     if (queue_head != NULL && queue_tail != NULL)  
  119.     {  
  120.         queue_tail->next = newwork;  
  121.         queue_tail = newwork;     
  122.     }  
  123.     else  
  124.     {  
  125.         //空队列  
  126.         queue_head = newwork;  
  127.         queue_tail = newwork;  
  128.     }  
  129.   
  130.     cur_queue_size++;  
  131.     pthread_mutex_unlock(&queue_lock);  
  132.     /*等待队列中有任务了,唤醒一个等待线程,注意如果所有线程都在忙碌,这句没有任何作用*/  
  133.     pthread_cond_signal(&(queue_cond));   
  134.     printf("add work returned!\n");  
  135.     return 0;  
  136. }  
  137.   
  138.   
  139. void* c_thread_pool::thread_routine(void *arg)  
  140. {  
  141.     c_thread_pool *pool = (c_thread_pool *)arg;  
  142.     int i = 0;  
  143.     while (1)  
  144.     {  
  145.         pthread_mutex_lock(&(pool->queue_lock));  
  146.         //如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意  
  147.          // pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁  
  148.   
  149.         //标注:注意这一如果任务队列不为空的话,while语句将被跳过,直接执行下面的调用。  
  150.         while (pool->cur_queue_size == 0 && pool->shutdown)  
  151.         {  
  152.             pthread_cond_wait(&(pool->queue_cond), &(pool->queue_lock));  
  153.         }  
  154.   
  155.   
  156.         //等待队列长度减去1,并取出链表中的头元素  
  157.         if (pool->cur_queue_size > 0 && pool->queue_head != NULL)  
  158.         {  
  159.             printf("IN THREAD ROUTINE size = %d && queue head is not NULL\n", pool->cur_queue_size);  
  160.             pool->cur_queue_size--;  
  161.             c_work *work = pool->queue_head;  
  162.             pool->queue_head = work->next;  
  163.             pthread_mutex_unlock(&(pool->queue_lock));  
  164.   
  165.             //调用回调函数,执行测试任务  
  166.             //////////////////////////////////////////  
  167.             (*(work->process))(work->arg);  
  168.             free(work);  
  169.             work = NULL;  
  170.         }  
  171.         else //不可达  
  172.         {  
  173.             pthread_mutex_unlock(&(pool->queue_lock));  
  174.         }  
  175.     }  
  176.       
  177. }  
  178.   
  179. c_thread_pool::~c_thread_pool()  
  180. {  
  181.     for (int i = 0; i < max_thread_num; ++i)   
  182.         pthread_cancel(threadid[i]);  
  183.     for (c_work *w_t = queue_head; w_t != NULL;)  
  184.     {  
  185.         c_work *temp = w_t->next;  
  186.         delete w_t;  
  187.         w_t = temp;  
  188.     }  
  189.     delete [] threadid;  
  190. }  

[cpp] view plaincopyprint?
  1. #include "___pool.h"  
  2.   
  3. struct adder  
  4. {  
  5.     int a;  
  6.     int b;  
  7. };  
  8.   
  9. void *process(void *add)  
  10. {  
  11.     struct adder *addp = (struct adder*)add;  
  12.     printf("-----in process:\n");  
  13.     printf("sum = %d + %d = %d\n", addp->a, addp->b, addp->a +  addp->b);  
  14.     return (void *)0;  
  15. }  
  16. int main(int argc, char **argv)  
  17. {  
  18.     if (argc < 2)  
  19.     {     
  20.         printf("input task num!\n");  
  21.         return -1;  
  22.     }  
  23.     int num = atoi(argv[1]);  
  24.     int i;  
  25.     c_thread_pool pool(10);           
  26.     struct adder add[num];  
  27.     for (i = 0; i < num; i++)  
  28.     {  
  29.         add[i].a = i + 1;  
  30.         add[i].b = i + 1;  
  31.     }  
  32.     c_worker wker[num];  
  33.     for (i = 0; i < num; i++)  
  34.     {  
  35.         wker[i].process = process;  
  36.         wker[i].arg = &add[i];  
  37.     }  
  38.     for (i = 0; i < num; i++)  
  39.     {  
  40.         pool.add_worker(wker[i]);  
  41.     }  
  42.     sleep(100);  
  43.     printf("\n");  
  44.     return 0;  
  45. }  


原文地址:http://blog.csdn.net/naturebe/article/details/7901130

[cpp] view plaincopyprint?
  1. ///////////////////////////////////////////////////////  
  2. //线程池类   
  3. ///////////////////////////////////////////////////////  
  4. #include <pthread.h>  
  5. #include <stdlib.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <assert.h>  
  9.   
  10. const int DEFAULT_MAX_THREAD_NUM = 10;  
  11. const int MAX_WORK_NUM = 100000;  
  12. //c_worker类  
  13. class c_work  
  14. {  
  15.     public:  
  16.         c_work():process(NULL), arg(NULL), next(NULL){}  
  17.         c_work(void *(*prcss)(void *), void *arg):  
  18.             process(prcss), arg(arg), next(NULL) {}  
  19.         ~c_work();   
  20.   
  21.         void *(*process)(void *);  
  22.         void *arg;  
  23.         unsigned char type; //最高位表示arg是否需要delete操作  
  24.         c_work *next;  
  25. };  
  26.   
  27. c_work::~c_work()  
  28. {  
  29.     unsigned char ifdel = type >> 7;  
  30.     if (ifdel)  
  31.     {  
  32.         delete arg;  
  33.         arg = NULL;  
  34.     }  
  35. }  
  36.   
  37. class c_thread_pool  
  38. {  
  39.     public:  
  40.         c_thread_pool();  
  41.         c_thread_pool(const int max_thread_num);  
  42.         ~c_thread_pool();  
  43.   
  44.         int add_work(c_work work);  
  45.         static void *thread_routine(void *arg);  
  46.   
  47.         pthread_mutex_t queue_lock;  
  48.         pthread_cond_t queue_cond;  
  49.   
  50. //  private:  
  51.         c_work *queue_head;  
  52.         c_work *queue_tail;  
  53.         int shutdown;  
  54.   
  55.         pthread_t *threadid;  
  56.         int max_thread_num;  
  57.         int cur_queue_size;  
  58. };  
  59.   
  60. c_thread_pool::c_thread_pool()  
  61. {  
  62.   
  63.     pthread_mutex_init(&queue_lock, NULL);  
  64.     pthread_cond_init(&queue_cond, NULL);  
  65.   
  66.     //工作队列初始化  
  67.     queue_head = NULL;  
  68.     queue_tail = NULL;  
  69.       
  70.     max_thread_num = max_thread_num;  
  71.     cur_queue_size = 0;  
  72.   
  73.     shutdown = 0;  
  74.   
  75.     max_thread_num = DEFAULT_MAX_THREAD_NUM;  
  76.     threadid = new pthread_t[max_thread_num];  
  77.     int i = 0;  
  78.   
  79.     for (i = 0; i < max_thread_num; i++)  
  80.     {  
  81.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  82.     }  
  83. }  
  84.   
  85.   
  86. c_thread_pool::c_thread_pool(int max_thread_num)  
  87. {  
  88.   
  89.     pthread_mutex_init(&queue_lock, NULL);  
  90.     pthread_cond_init(&queue_cond, NULL);  
  91.   
  92.     //工作队列初始化  
  93.     queue_head = NULL;  
  94.     queue_tail = NULL;  
  95.       
  96.     max_thread_num = max_thread_num;  
  97.     cur_queue_size = 0;  
  98.   
  99.     threadid = new pthread_t[max_thread_num];  
  100.     int i = 0;  
  101.     for (i = 0; i < max_thread_num; i++)  
  102.     {  
  103.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  104.     }  
  105. }  
  106.   
  107. /*向线程池中的任务队列加入任务*/  
  108. int c_thread_pool::add_work(c_work work)  
  109. {  
  110.     c_work *newwork = new c_work;  
  111.     newwork->process = work.process;  
  112.     newwork->arg = work.arg;  
  113.     newwork->next = NULL;  
  114.   
  115.     pthread_mutex_lock(&queue_lock);  
  116.       
  117.     /*将任务加入到等待队列中*/  
  118.     if (queue_head != NULL && queue_tail != NULL)  
  119.     {  
  120.         queue_tail->next = newwork;  
  121.         queue_tail = newwork;     
  122.     }  
  123.     else  
  124.     {  
  125.         //空队列  
  126.         queue_head = newwork;  
  127.         queue_tail = newwork;  
  128.     }  
  129.   
  130.     cur_queue_size++;  
  131.     pthread_mutex_unlock(&queue_lock);  
  132.     /*等待队列中有任务了,唤醒一个等待线程,注意如果所有线程都在忙碌,这句没有任何作用*/  
  133.     pthread_cond_signal(&(queue_cond));   
  134.     printf("add work returned!\n");  
  135.     return 0;  
  136. }  
  137.   
  138.   
  139. void* c_thread_pool::thread_routine(void *arg)  
  140. {  
  141.     c_thread_pool *pool = (c_thread_pool *)arg;  
  142.     int i = 0;  
  143.     while (1)  
  144.     {  
  145.         pthread_mutex_lock(&(pool->queue_lock));  
  146.         //如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意  
  147.          // pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁  
  148.   
  149.         //标注:注意这一如果任务队列不为空的话,while语句将被跳过,直接执行下面的调用。  
  150.         while (pool->cur_queue_size == 0 && pool->shutdown)  
  151.         {  
  152.             pthread_cond_wait(&(pool->queue_cond), &(pool->queue_lock));  
  153.         }  
  154.   
  155.   
  156.         //等待队列长度减去1,并取出链表中的头元素  
  157.         if (pool->cur_queue_size > 0 && pool->queue_head != NULL)  
  158.         {  
  159.             printf("IN THREAD ROUTINE size = %d && queue head is not NULL\n", pool->cur_queue_size);  
  160.             pool->cur_queue_size--;  
  161.             c_work *work = pool->queue_head;  
  162.             pool->queue_head = work->next;  
  163.             pthread_mutex_unlock(&(pool->queue_lock));  
  164.   
  165.             //调用回调函数,执行测试任务  
  166.             //////////////////////////////////////////  
  167.             (*(work->process))(work->arg);  
  168.             free(work);  
  169.             work = NULL;  
  170.         }  
  171.         else //不可达  
  172.         {  
  173.             pthread_mutex_unlock(&(pool->queue_lock));  
  174.         }  
  175.     }  
  176.       
  177. }  
  178.   
  179. c_thread_pool::~c_thread_pool()  
  180. {  
  181.     for (int i = 0; i < max_thread_num; ++i)   
  182.         pthread_cancel(threadid[i]);  
  183.     for (c_work *w_t = queue_head; w_t != NULL;)  
  184.     {  
  185.         c_work *temp = w_t->next;  
  186.         delete w_t;  
  187.         w_t = temp;  
  188.     }  
  189.     delete [] threadid;  
  190. }  
0 0