编写了一个最基本的线程池类,处理用c_work表示的工作任务。

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