一个Linux下C线程池的实现

http://blog.csdn.net/zouxinfox/article/details/3560891

 什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了。

    下面是Linux系统下用C语言创建的一个线程池。线程池会维护一个任务链表(每个CThread_worker结构就是一个任务)。
    pool_init()函数预先创建好max_thread_num个线程,每个线程执thread_routine ()函数。该函数中

  1. while (pool->cur_queue_size == 0)
  2. {
  3.       pthread_cond_wait (&(pool->queue_ready),&(pool->queue_lock));
  4. }
表示如果任务链表中没有任务,则该线程出于阻塞等待状态。否则从队列中取出任务并执行。
    
    pool_add_worker()函数向线程池的任务链表中加入一个任务,加入后通过调用pthread_cond_signal (&(pool->queue_ready))唤醒一个出于阻塞状态的线程(如果有的话)。
    
    pool_destroy ()函数用于销毁线程池,线程池任务链表中的任务不会再被执行,但是正在运行的线程会一直把任务运行完后再退出。
    
    下面贴出完整代码
  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. *线程池里所有运行和等待的任务都是一个CThread_worker
  9. *由于所有任务都在链表里,所以是一个链表结构
  10. */
  11. typedef struct worker
  12. {
  13.     /*回调函数,任务运行时会调用此函数,注意也可声明成其它形式*/
  14.     void *(*process) (void *arg);
  15.     void *arg;/*回调函数的参数*/
  16.     struct worker *next;
  17. } CThread_worker;

  18. /*线程池结构*/
  19. typedef struct
  20. {
  21.     pthread_mutex_t queue_lock;
  22.     pthread_cond_t queue_ready;
  23.     /*链表结构,线程池中所有等待任务*/
  24.     CThread_worker *queue_head;
  25.     /*是否销毁线程池*/
  26.     int shutdown;
  27.     pthread_t *threadid;
  28.     /*线程池中允许的活动线程数目*/
  29.     int max_thread_num;
  30.     /*当前等待队列的任务数目*/
  31.     int cur_queue_size;
  32. } CThread_pool;

  33. int pool_add_worker (void *(*process) (void *arg), void *arg);
  34. void *thread_routine (void *arg);

  35. static CThread_pool *pool = NULL;
  36. void
  37. pool_init (int max_thread_num)
  38. {
  39.     pool = (CThread_pool *) malloc (sizeof (CThread_pool));
  40.     pthread_mutex_init (&(pool->queue_lock), NULL);
  41.     pthread_cond_init (&(pool->queue_ready), NULL);
  42.     pool->queue_head = NULL;
  43.     pool->max_thread_num = max_thread_num;
  44.     pool->cur_queue_size = 0;
  45.     pool->shutdown = 0;
  46.     pool->threadid =
  47.         (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));
  48.     int i = 0;
  49.     for (i = 0; i < max_thread_num; i++)
  50.     { 
  51.         pthread_create (&(pool->threadid[i]), NULL, thread_routine,
  52.                 NULL);
  53.     }
  54. }

  55. /*向线程池中加入任务*/
  56. int
  57. pool_add_worker (void *(*process) (void *arg), void *arg)
  58. {
  59.     /*构造一个新任务*/
  60.     CThread_worker *newworker =
  61.         (CThread_worker *) malloc (sizeof (CThread_worker));
  62.     newworker->process = process;
  63.     newworker->arg = arg;
  64.     newworker->next = NULL;/*别忘置空*/
  65.     pthread_mutex_lock (&(pool->queue_lock));
  66.     /*将任务加入到等待队列中*/
  67.     CThread_worker *member = pool->queue_head;
  68.     if (member != NULL)
  69.     {
  70.         while (member->next != NULL)
  71.             member = member->next;
  72.         member->next = newworker;
  73.     }
  74.     else
  75.     {
  76.         pool->queue_head = newworker;
  77.     }
  78.     assert (pool->queue_head != NULL);
  79.     pool->cur_queue_size++;
  80.     pthread_mutex_unlock (&(pool->queue_lock));
  81.     /*好了,等待队列中有任务了,唤醒一个等待线程;
  82.     注意如果所有线程都在忙碌,这句没有任何作用*/
  83.     pthread_cond_signal (&(pool->queue_ready));
  84.     return 0;
  85. }

  86. /*销毁线程池,等待队列中的任务不会再被执行,但是正在运行的线程会一直
  87. 把任务运行完后再退出*/
  88. int
  89. pool_destroy ()
  90. {
  91.     if (pool->shutdown)
  92.         return -1;/*防止两次调用*/
  93.     pool->shutdown = 1;
  94.     /*唤醒所有等待线程,线程池要销毁了*/
  95.     pthread_cond_broadcast (&(pool->queue_ready));
  96.     /*阻塞等待线程退出,否则就成僵尸了*/
  97.     int i;
  98.     for (i = 0; i < pool->max_thread_num; i++)
  99.         pthread_join (pool->threadid[i], NULL);
  100.     free (pool->threadid);
  101.     /*销毁等待队列*/
  102.     CThread_worker *head = NULL;
  103.     while (pool->queue_head != NULL)
  104.     {
  105.         head = pool->queue_head;
  106.         pool->queue_head = pool->queue_head->next;
  107.         free (head);
  108.     }
  109.     /*条件变量和互斥量也别忘了销毁*/
  110.     pthread_mutex_destroy(&(pool->queue_lock));
  111.     pthread_cond_destroy(&(pool->queue_ready));
  112.     
  113.     free (pool);
  114.     /*销毁后指针置空是个好习惯*/
  115.     pool=NULL;
  116.     return 0;
  117. }

  118. void *
  119. thread_routine (void *arg)
  120. {
  121.     printf ("starting thread 0x%x/n", pthread_self ());
  122.     while (1)
  123.     {
  124.         pthread_mutex_lock (&(pool->queue_lock));
  125.         /*如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意
  126.         pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁*/
  127.         while (pool->cur_queue_size == 0 && !pool->shutdown)
  128.         {
  129.             printf ("thread 0x%x is waiting/n", pthread_self ());
  130.             pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock));
  131.         }
  132.         /*线程池要销毁了*/
  133.         if (pool->shutdown)
  134.         {
  135.             /*遇到break,continue,return等跳转语句,千万不要忘记先解锁*/
  136.             pthread_mutex_unlock (&(pool->queue_lock));
  137.             printf ("thread 0x%x will exit/n", pthread_self ());
  138.             pthread_exit (NULL);
  139.         }
  140.         printf ("thread 0x%x is starting to work/n", pthread_self ());
  141.         /*assert是调试的好帮手*/
  142.         assert (pool->cur_queue_size != 0);
  143.         assert (pool->queue_head != NULL);
  144.         
  145.         /*等待队列长度减去1,并取出链表中的头元素*/
  146.         pool->cur_queue_size--;
  147.         CThread_worker *worker = pool->queue_head;
  148.         pool->queue_head = worker->next;
  149.         pthread_mutex_unlock (&(pool->queue_lock));
  150.         /*调用回调函数,执行任务*/
  151.         (*(worker->process)) (worker->arg);
  152.         free (worker);
  153.         worker = NULL;
  154.     }
  155.     /*这一句应该是不可达的*/
  156.     pthread_exit (NULL);
  157. }
    下面是测试代码
  1. void *
  2. myprocess (void *arg)
  3. {
  4.     printf ("threadid is 0x%x, working on task %d/n", pthread_self (),*(int *) arg);
  5.     sleep (1);/*休息一秒,延长任务的执行时间*/
  6.     return NULL;
  7. }
  8. int
  9. main (int argc, char **argv)
  10. {
  11.     pool_init (3);/*线程池中最多三个活动线程*/
  12.     
  13.     /*连续向池中投入10个任务*/
  14.     int *workingnum = (int *) malloc (sizeof (int) * 10);
  15.     int i;
  16.     for (i = 0; i < 10; i++)
  17.     {
  18.         workingnum[i] = i;
  19.         pool_add_worker (myprocess, &workingnum[i]);
  20.     }
  21.     /*等待所有任务完成*/
  22.     sleep (5);
  23.     /*销毁线程池*/
  24.     pool_destroy ();
  25.     free (workingnum);
  26.     return 0;
  27. }
将上述所有代码放入threadpool.c文件中,
在Linux输入编译命令
$ gcc -o threadpool threadpool.c -lpthread

以下是运行结果
starting thread 0xb7df6b90
thread 0xb7df6b90 is waiting
starting thread 0xb75f5b90
thread 0xb75f5b90 is waiting
starting thread 0xb6df4b90
thread 0xb6df4b90 is waiting
thread 0xb7df6b90 is starting to work
threadid is 0xb7df6b90, working on task 0
thread 0xb75f5b90 is starting to work
threadid is 0xb75f5b90, working on task 1
thread 0xb6df4b90 is starting to work
threadid is 0xb6df4b90, working on task 2
thread 0xb7df6b90 is starting to work
threadid is 0xb7df6b90, working on task 3
thread 0xb75f5b90 is starting to work
threadid is 0xb75f5b90, working on task 4
thread 0xb6df4b90 is starting to work
threadid is 0xb6df4b90, working on task 5
thread 0xb7df6b90 is starting to work
threadid is 0xb7df6b90, working on task 6
thread 0xb75f5b90 is starting to work
threadid is 0xb75f5b90, working on task 7
thread 0xb6df4b90 is starting to work
threadid is 0xb6df4b90, working on task 8
thread 0xb7df6b90 is starting to work
threadid is 0xb7df6b90, working on task 9
thread 0xb75f5b90 is waiting
thread 0xb6df4b90 is waiting
thread 0xb7df6b90 is waiting
thread 0xb75f5b90 will exit
thread 0xb6df4b90 will exit
thread 0xb7df6b90 will exit


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/383941.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Gym100917 A - Abstract Picture

模拟赛的时候看这道题没有什么头绪&#xff0c;当时有点晕&#xff0c;感冒还没有好&#xff0c;回来以后瞟了一眼题解就明白了&#xff0c;自己实现了一下&#xff0c;也没有很复杂。大概的思路就像拓扑排序一样&#xff0c;需要理解因为涂的是有顺序的&#xff0c;所以我们总…

linux进程通信---几个发送信号的函数(kill,raise,alarm,pause)

http://blog.csdn.net/zzyoucan/article/details/9235685 信号&#xff1a;信号是unix中最古老的进程通信的一种方式&#xff0c;他是软件层次上对中断机制的模拟&#xff0c;是一种异步通信方式&#xff0c;信号可以实现用户空间进程和内核空间进程的交互&#xff0c;内核进程…

数据库以及表的基本操作

一.数据库的操作 create database[if not exists]数据库名; 创建一个名字为company2的使用utf8忽略大小写的数据库 create database company charsetutf8 collate utf8_general_ci; 创建一个数据库区分大小写 create database company1 charsetutf8 collate utf8_general_bin;…

linux 网络编程:使用两线程实现socket同时收发数据

http://blog.csdn.net/li_wen01/article/details/52665505 工作中最近有使用到socket 向客户端同时发送和接收数据&#xff0c;因为是嵌入式linux设备&#xff0c;且要求只能同时一个客户端连接该端口。考虑到节省系统资源&#xff0c;只创建了两个线程分别实现服务端的收发数据…

CF Gym102059 H. Fractions

题目要求找到给定区间的化简后分子分母的和小于1000的数字的个数 我的想法是先找到所有的满足要求的最简分数(总数不超过1e6,而且远小于),然后对询问查找每个最简分数出现的次数. #include<cstdio> #include<cstring> #include<algorithm> #include<cli…

C语言calloc()函数:分配内存空间并初始化

http://c.biancheng.net/cpp/html/134.html 头文件&#xff1a;#include <stdlib.h> calloc() 函数用来动态地分配内存空间并初始化为 0&#xff0c;其原型为&#xff1a; void* calloc (size_t num, size_t size); calloc() 在内存中动态地分配 num 个长度为 siz…

CF Gym100917 C

要找到和为给定值的所有的等比数列. 1肯定是要特判一下. 我的想法是先找到所有等比为1的,等比为1就是将这个数分为相同的一些数,总共就是这个数的所有约数个数减一(有一个约数为1,题目要求至少分成两个数). 对于其他的等比不为1 的,用等比数列的求和公式暴力一发就行了. #i…

多路转接select1

高级IO 通常情况下所有的 IO 都可以分为两步来完成, 第一步等待, 第二步数据搬迁, 为了提高 IO 效率通常所运用的方法就是减少等待的时间 举个钓鱼的例子 现在有五个人张三, 李四, 王五, 赵六, 钱七. 它们五个人来到湖边来钓鱼. 而它们五个人的钓鱼方各不相同. 张三钓鱼方法…

UVa11181

题目要求条件概率,用贝叶斯公式我们很容易得到我们需要求r个人买东西的概率和每个人买东西的条件下其他r-1个人买东西的概率.我们递归枚举,每当枚举到r个人买东西的时候,我们加入到r个人买东西的概率中(全概率公式),然后对于这r个人,除去自己买东西的概率就是其他r-1个人买东西…

Linux epoll模型

http://www.cnblogs.com/venow/archive/2012/11/30/2790031.html 定义&#xff1a; epoll是Linux内核为处理大批句柄而作改进的poll&#xff0c;是Linux下多路复用IO接口select/poll的增强版本&#xff0c;它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利…

UVa11572

书上把这种问题叫做滑动窗口问题. 我的想法是先进行离散化,然后用一个数组记录元素出现的位置,如果判断某个元素已经出现,就将左端点移到上次出现的位置的后面.每次出现重复元素的时候判断一下答案.我觉得这样的复杂度是最低的. #include<cstdio> #include<cstring&…

Linux IO模式及 select、poll、epoll详解

https://segmentfault.com/a/1190000003063859 同步IO和异步IO&#xff0c;阻塞IO和非阻塞IO分别是什么&#xff0c;到底有什么区别&#xff1f;不同的人在不同的上下文下给出的答案是不同的。所以先限定一下本文的上下文。 本文讨论的背景是Linux环境下的network IO。一 概念…

mysql思维导图

后期会不断进行更新

CF Gym 101630 B Box

题目的意思大概就是给一个长方体的长宽高,问他能不能用一个w*h的纸剪出来,就是说展开图的长宽能不能比给定的小. 题目给了11中展开图的拓扑结构,我觉得这个很关键,要是题目没有给这个我可能想不到那么全面,不过题目已经给了我就分析那11个图形,发现展开图的长宽大概分为三类 …

C++第一节课

C数据类型 几个概念 命名空间是C标准库引入的,其中命名空间可以解决变量冲突问题,当出现局部变量和全局变量同名的时候, 局部变量优先被访问.同时命名空间的格式如同一下代码 namespace name1 { int a 0; }namespace name2 { int a 2; } 注意C中的所有组件都是在一个叫做s…

【C/C++】关键字static

http://blog.csdn.net/woxiaohahaa/article/details/51014224 参考自&#xff1a;http://www.cnblogs.com/biyeymyhjob/archive/2012/07/19/2598815.html &#xff08;华山大师兄&#xff09; 这里我们只讨论了C语言的static 首先我们回顾一下各种变量在内存中的位置&#xff1…

HDU5391威尔逊定理

威尔逊定理 当且仅当p为素数,p | (p-1)!1 若p为合数,则pa*b;如果a!b,那么p|(p-1)!, 如果ab,如果p为4,那么p|(p-1)!2,如果p大于4,那么sqrt和sqrt(2q)肯定属于(p-1)!中,可以整除 #include<cstdio> #include<cstring> #include<algorithm> #include<climit…

C++的基本认识

简单介绍C 语言特点 支持数据封装和数据隐藏 在C中&#xff0c;类是支持数据封装的工具&#xff0c;对象则是数据封装的实现。C通过建立用户定义类支持数据封装和数据隐藏。 在面向对象的程序设计中&#xff0c;将数据和对该数据进行合法操作的函数封装在一起作为一个类的定…

OD 投篮大赛

/*** 题目描述* 你现在是一场采用特殊赛制投篮大赛的记录员。这场比赛由若干回合组成&#xff0c;过去几回合的得分可能会影响以后几回合的得分。* 比赛开始时&#xff0c;记录时空白的。你会得到一个记录操作的字符串列表aops&#xff0c;其中ops[i]是你需要记录的第i项操作&a…

IO多路复用之epoll总结

http://www.cnblogs.com/Anker/p/3263780.html 1、基本知识 epoll是在2.6内核中提出的&#xff0c;是之前的select和poll的增强版本。相对于select和poll来说&#xff0c;epoll更加灵活&#xff0c;没有描述符限制。epoll使用一个文件描述符管理多个描述符&#xff0c;将用户关…