zoukankan      html  css  js  c++  java
  • 线程池



    1. #include <stdio.h>   
    2. #include <stdlib.h>   
    3. #include <unistd.h>   
    4. #include <sys/types.h>   
    5. #include <pthread.h>   
    6. #include <assert.h>   
    7.   
    8. /*  
    9. *线程池里所有运行和等待的任务都是一个CThread_worker  
    10. *由于所有任务都在链表里,所以是一个链表结构  
    11. */   
    12. typedef struct worker   
    13. {   
    14.     /*回调函数,任务运行时会调用此函数,注意也可声明成其它形式*/   
    15.     void *(*process) (void *arg);   
    16.     void *arg;/*回调函数的参数*/   
    17.     struct worker *next;   
    18.   
    19. } CThread_worker;   
    20.     
    21. /*线程池结构*/   
    22. typedef struct   
    23. {   
    24.     pthread_mutex_t queue_lock;   
    25.     pthread_cond_t queue_ready;   
    26.   
    27.     /*链表结构,线程池中所有等待任务*/   
    28.     CThread_worker *queue_head;   
    29.   
    30.     /*是否销毁线程池*/   
    31.     int shutdown;   
    32.     pthread_t *threadid;   
    33.     /*线程池中允许的活动线程数目*/   
    34.     int max_thread_num;   
    35.     /*当前等待队列的任务数目*/   
    36.     int cur_queue_size;   
    37.   
    38. } CThread_pool;   /*定义线程池*/
    39.     
    40. int pool_add_worker (void *(*process) (void *arg), void *arg);   
    41. void *thread_routine (void *arg);     /*routine:程序*/
    42.   
    43. static CThread_pool *pool = NULL;    /*声明 pool为线程池指针*/
    44. void pool_init (int max_thread_num)   /*线程池初始化*/
    45. {   
    46.     pool = (CThread_pool *) malloc (sizeof (CThread_pool));   
    47.     /*线程池pool,初始化互斥量*/
    48.     pthread_mutex_init (&(pool->queue_lock), NULL);   
    49.     /*线程池pool,初始化条件变量*/  
    50.     pthread_cond_init (&(pool->queue_ready), NULL);   
    51.    /
    52.     pool->queue_head = NULL;    /*任务队列头*/
    53.   
    54.     pool->max_thread_num = max_thread_num;   /*最大线程数*/
    55.     pool->cur_queue_size = 0;    /*当前任务数*/
    56.   
    57.     pool->shutdown = 0;   /*是否销毁*/
    58.   
    59.     pool->threadid =   
    60.         (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));   /*申请线程地址空间*/
    61.     int i = 0;   
    62.     for (i = 0; i < max_thread_num; i++)   
    63.     {     /*把线程全部创建出来*/
    64.         pthread_create (&(pool->threadid[i]), NULL, thread_routine, NULL);   
    65.     }   
    66. }     
    67. /*向线程池中加入任务*/   
    68. int   
    69. pool_add_worker (void *(*process) (void *arg), void *arg)   /*线程添加*/
    70. {   
    71.     /*构造一个新任务*/   
    72.     CThread_worker *newworker =                   
    73.         (CThread_worker *) malloc (sizeof (CThread_worker));    /*将任务传递进来*/  
    74.     newworker->process = process;    /*任务函数*/  
    75.     newworker->arg = arg;            /*函数参数*/  
    76.     newworker->next = NULL;/*别忘置空*/   
    77.   
    78.     pthread_mutex_lock (&(pool->queue_lock));   /*添加新任务时需要加锁,防止其他线程改变公用变量*/
    79.     /*将任务加入到等待队列中*/   
    80.     CThread_worker *member = pool->queue_head;   /*任务队列传入*/
    81.     if (member != NULL)   /*任务队列不为空*/
    82.     {   
    83.         while (member->next != NULL)    /*任务队列链表遍历到最后位置*/
    84.             member = member->next;   
    85.         member->next = newworker;    /*新任务添加到队列链表中*/
    86.     }   
    87.     else   /*任务队列为空*/
    88.     {   
    89.         pool->queue_head = newworker;   /*新任务添加到队首*/
    90.     }   
    91.   
    92.     assert (pool->queue_head != NULL);  /*任务添加过程结束后,任务队列仍为空,报错*/ 
    93.   
    94.     pool->cur_queue_size++;    /*池内当前任务数+1*/
    95.     pthread_mutex_unlock (&(pool->queue_lock));     /*解锁*/
    96.     pthread_cond_signal (&(pool->queue_ready));/*每次等待队列中有任务加入,都会试图唤醒一个等待线程;如果所有线程都在忙碌,这句没有任何作用*/
    97.     return 0;   
    98. }   
    99.   
    100. /*销毁线程池,等待队列中的任务不会再被执行,但是正在运行的线程会一直  
    101. 把任务运行完后再退出*/   
    102. int pool_destroy ()        /*线程池销毁*/
    103. {   
    104.     if (pool->shutdown)   
    105.         return -1;/*防止两次调用*/   
    106.     pool->shutdown = 1;   
    107.   
    108.     /*唤醒所有等待线程,线程池要销毁了*/   
    109.     pthread_cond_broadcast (&(pool->queue_ready));   
    110.   
    111.     /*阻塞等待线程退出,否则就成僵尸了*/   
    112.     int i;   
    113.     for (i = 0; i < pool->max_thread_num; i++)   
    114.         pthread_join (pool->threadid[i], NULL);   
    115.     free (pool->threadid);    /*释放每一个线程空间*/
    116.   
    117.     /*销毁等待队列*/   
    118.     CThread_worker *head = NULL;   
    119.     while (pool->queue_head != NULL)   
    120.     {   
    121.         head = pool->queue_head;   
    122.         pool->queue_head = pool->queue_head->next;   
    123.         free (head);    /*释放任务队列空间*/
    124.     }   
    125.     /*条件变量和互斥量也别忘了销毁*/   
    126.     pthread_mutex_destroy(&(pool->queue_lock));   
    127.     pthread_cond_destroy(&(pool->queue_ready));   
    128.        
    129.     free (pool);    /*释放线程池空间*/
    130.     /*销毁后指针置空是个好习惯*/   
    131.     pool=NULL;   
    132.     return 0;   
    133. }    
    134. void * thread_routine (void *arg)       /*线程调用函数*/   
    135. {   
    136.     printf ("starting thread 0x%x ", pthread_self ());   
    137.     while (1)   
    138.     {   
    139.         pthread_mutex_lock (&(pool->queue_lock));   
    140.         /*如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意  
    141.         pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁*/   
    142.         while (pool->cur_queue_size == 0 && !pool->shutdown)   
    143.         {   
    144.             printf ("thread 0x%x is waiting ", pthread_self ());   
    145.             pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock));/*如果暂时没有任务,阻塞在此处,等待唤醒,防止不停查询,浪费CPU*/ 
    146.         }   
    147.   
    148.         /*线程池要销毁了*/   
    149.         if (pool->shutdown)   
    150.         {   
    151.             /*遇到break,continue,return等跳转语句,千万不要忘记先解锁*/   
    152.             pthread_mutex_unlock (&(pool->queue_lock));   
    153.             printf ("thread 0x%x will exit ", pthread_self ());   
    154.             pthread_exit (NULL);   
    155.         }   
    156.   
    157.         printf ("thread 0x%x is starting to work ", pthread_self ());   
    158.   
    159.         /*assert是调试的好帮手*/   
    160.         assert (pool->cur_queue_size != 0);   
    161.         assert (pool->queue_head != NULL);   
    162.            
    163.         /*等待队列长度减去1,并取出链表中的头元素*/   
    164.         pool->cur_queue_size--;   
    165.         CThread_worker *worker = pool->queue_head;   /*每次执行的都是任务队列中第一个任务*/
    166.         pool->queue_head = worker->next;    /*任务队列头下移一个*/
    167.         pthread_mutex_unlock (&(pool->queue_lock));   
    168.   
    169.         /*调用回调函数,执行任务*/   
    170.         (*(worker->process)) (worker->arg);   /*开始执行取出来的任务*/
    171.         free (worker);   
    172.         worker = NULL;   
    173.     }   
    174.     /*这一句应该是不可达的*/   
    175.     pthread_exit (NULL);   
    176. }  
    177.   
    178. void * myprocess (void *arg)   /*任务线程*/
    179. {   
    180.     printf ("threadid is 0x%x, working on task %d ", pthread_self (),*(int *) arg);   
    181.     sleep (1);/*休息一秒,延长任务的执行时间*/   
    182.     return NULL;   
    183. }   
    184.   
    185. int main (int argc, char **argv)   
    186. {   
    187.     pool_init (3);/*线程池中最多三个活动线程*/   
    188.        
    189.     /*连续向池中投入10个任务*/   
    190.     int *workingnum = (int *) malloc (sizeof (int) * 10);       /*任务编号*/  
    191.     int i;   
    192.     for (i = 0; i < 10; i++)   
    193.     {   
    194.         workingnum[i] = i;   
    195.         pool_add_worker (myprocess, &workingnum[i]);    /*任务添加进等待队列*/  
    196.     }   
    197.     /*等待所有任务完成*/   
    198.     sleep (5);   
    199.     /*销毁线程池*/   
    200.     pool_destroy ();   
    201.   
    202.     free (workingnum);   
    203.     return 0; 

           [root@localhost code]# ./thread_pool

    starting thread 0xb63bab70
    thread 0xb63bab70 is starting to work
    threadid is 0xb63bab70, working on task 0
    starting thread 0xb6dbbb70
    thread 0xb6dbbb70 is starting to work
    threadid is 0xb6dbbb70, working on task 1
    starting thread 0xb77bcb70
    thread 0xb77bcb70 is starting to work
    threadid is 0xb77bcb70, working on task 2
    thread 0xb63bab70 is starting to work
    threadid is 0xb63bab70, working on task 3
    thread 0xb6dbbb70 is starting to work
    threadid is 0xb6dbbb70, working on task 4
    thread 0xb77bcb70 is starting to work
    threadid is 0xb77bcb70, working on task 5
    thread 0xb63bab70 is starting to work
    threadid is 0xb63bab70, working on task 6
    thread 0xb6dbbb70 is starting to work
    threadid is 0xb6dbbb70, working on task 7
    thread 0xb77bcb70 is starting to work
    threadid is 0xb77bcb70, working on task 8
    thread 0xb63bab70 is starting to work
    threadid is 0xb63bab70, working on task 9
    thread 0xb6dbbb70 is waiting
    thread 0xb77bcb70 is waiting
    thread 0xb63bab70 is waiting
    thread 0xb6dbbb70 will exit
    thread 0xb77bcb70 will exit
    thread 0xb63bab70 will exit

    [root@localhost code]#


  • 相关阅读:
    区间贪心问题小结(区间选点,区间覆盖,区间选取)
    Poj-3630(字典树,水题)
    G
    hdu3460(字典树)
    HDU 5512 Pagodas(2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))
    Python项目之爬取斗图网所有图片
    Python学习笔记
    c重定向函数
    3110: [Zjoi2013]K大数查询
    4826: [Hnoi2017]影魔
  • 原文地址:https://www.cnblogs.com/liuchengchuxiao/p/4329913.html
Copyright © 2011-2022 走看看