Linux平台下线程池的原理及实现
转自:http://blog.csdn.net/lmh12506/article/details/7753952
前段时间在github上开了个库,准备实现自己的线程池的,因为换工作的事,一直也没有实现,参考这篇文章准备着手实现一下。
什么时候需要创建线程池呢?简单的说,如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了。
下面是Linux系统下用C语言创建的一个线程池。线程池会维护一个任务链表(每个CThread_worker结构就是一个任务)。
pool_init()函数预先创建好max_thread_num个线程,每个线程执thread_routine ()函数。该函数中
- while (pool->cur_queue_size == 0)
- {
- pthread_cond_wait (&(pool->queue_ready),&(pool->queue_lock));
- }
表示如果任务链表中没有任务,则该线程出于阻塞等待状态。否则从队列中取出任务并执行。
pool_add_worker()函数向线程池的任务链表中加入一个任务,加入后通过调用pthread_cond_signal (&(pool->queue_ready))唤醒一个出于阻塞状态的线程(如果有的话)。
pool_destroy ()函数用于销毁线程池,线程池任务链表中的任务不会再被执行,但是正在运行的线程会一直把任务运行完后再退出。
下面贴出完整代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <pthread.h>
- #include <assert.h>
- /*
- *线程池里所有运行和等待的任务都是一个CThread_worker
- *由于所有任务都在链表里,所以是一个链表结构
- */
- typedef struct worker
- {
- /*回调函数,任务运行时会调用此函数,注意也可声明成其它形式*/
- void *(*process) (void *arg);
- void *arg;/*回调函数的参数*/
- struct worker *next;
- } CThread_worker;
- /*线程池结构*/
- typedef struct
- {
- pthread_mutex_t queue_lock;
- pthread_cond_t queue_ready;
- /*链表结构,线程池中所有等待任务*/
- CThread_worker *queue_head;
- /*是否销毁线程池*/
- int shutdown;
- pthread_t *threadid;
- /*线程池中允许的活动线程数目*/
- int max_thread_num;
- /*当前等待队列的任务数目*/
- int cur_queue_size;
- } CThread_pool;
- int pool_add_worker (void *(*process) (void *arg), void *arg);
- void *thread_routine (void *arg);
- static CThread_pool *pool = NULL;
- void
- pool_init (int max_thread_num)
- {
- pool = (CThread_pool *) malloc (sizeof (CThread_pool));
- pthread_mutex_init (&(pool->queue_lock), NULL);
- pthread_cond_init (&(pool->queue_ready), NULL);
- pool->queue_head = NULL;
- pool->max_thread_num = max_thread_num;
- pool->cur_queue_size = 0;
- pool->shutdown = 0;
- pool->threadid =
- (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));
- int i = 0;
- for (i = 0; i < max_thread_num; i++)
- {
- pthread_create (&(pool->threadid[i]), NULL, thread_routine,
- NULL);
- }
- }
- /*向线程池中加入任务*/
- int
- pool_add_worker (void *(*process) (void *arg), void *arg)
- {
- /*构造一个新任务*/
- CThread_worker *newworker =
- (CThread_worker *) malloc (sizeof (CThread_worker));
- newworker->process = process;
- newworker->arg = arg;
- newworker->next = NULL;/*别忘置空*/
- pthread_mutex_lock (&(pool->queue_lock));
- /*将任务加入到等待队列中*/
- CThread_worker *member = pool->queue_head;
- if (member != NULL)
- {
- while (member->next != NULL)
- member = member->next;
- member->next = newworker;
- }
- else
- {
- pool->queue_head = newworker;
- }
- assert (pool->queue_head != NULL);
- pool->cur_queue_size++;
- pthread_mutex_unlock (&(pool->queue_lock));
- /*好了,等待队列中有任务了,唤醒一个等待线程;
- 注意如果所有线程都在忙碌,这句没有任何作用*/
- pthread_cond_signal (&(pool->queue_ready));
- return 0;
- }
- /*销毁线程池,等待队列中的任务不会再被执行,但是正在运行的线程会一直
- 把任务运行完后再退出*/
- int
- pool_destroy ()
- {
- if (pool->shutdown)
- return -1;/*防止两次调用*/
- pool->shutdown = 1;
- /*唤醒所有等待线程,线程池要销毁了*/
- pthread_cond_broadcast (&(pool->queue_ready));
- /*阻塞等待线程退出,否则就成僵尸了*/
- int i;
- for (i = 0; i < pool->max_thread_num; i++)
- pthread_join (pool->threadid[i], NULL);
- free (pool->threadid);
- /*销毁等待队列*/
- CThread_worker *head = NULL;
- while (pool->queue_head != NULL)
- {
- head = pool->queue_head;
- pool->queue_head = pool->queue_head->next;
- free (head);
- }
- /*条件变量和互斥量也别忘了销毁*/
- pthread_mutex_destroy(&(pool->queue_lock));
- pthread_cond_destroy(&(pool->queue_ready));
- free (pool);
- /*销毁后指针置空是个好习惯*/
- pool=NULL;
- return 0;
- }
- void *
- thread_routine (void *arg)
- {
- printf ("starting thread 0x%x\n", pthread_self ());
- while (1)
- {
- pthread_mutex_lock (&(pool->queue_lock));
- /*如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意
- pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁*/
- while (pool->cur_queue_size == 0 && !pool->shutdown)
- {
- printf ("thread 0x%x is waiting\n", pthread_self ());
- pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock));
- }
- /*线程池要销毁了*/
- if (pool->shutdown)
- {
- /*遇到break,continue,return等跳转语句,千万不要忘记先解锁*/
- pthread_mutex_unlock (&(pool->queue_lock));
- printf ("thread 0x%x will exit\n", pthread_self ());
- pthread_exit (NULL);
- }
- printf ("thread 0x%x is starting to work\n", pthread_self ());
- /*assert是调试的好帮手*/
- assert (pool->cur_queue_size != 0);
- assert (pool->queue_head != NULL);
- /*等待队列长度减去1,并取出链表中的头元素*/
- pool->cur_queue_size--;
- CThread_worker *worker = pool->queue_head;
- pool->queue_head = worker->next;
- pthread_mutex_unlock (&(pool->queue_lock));
- /*调用回调函数,执行任务*/
- (*(worker->process)) (worker->arg);
- free (worker);
- worker = NULL;
- }
- /*这一句应该是不可达的*/
- pthread_exit (NULL);
- }
下面是测试代码
- void *
- myprocess (void *arg)
- {
- printf ("threadid is 0x%x, working on task %d\n", pthread_self (),*(int *) arg);
- sleep (1);/*休息一秒,延长任务的执行时间*/
- return NULL;
- }
- int
- main (int argc, char **argv)
- {
- pool_init (3);/*线程池中最多三个活动线程*/
- /*连续向池中投入10个任务*/
- int *workingnum = (int *) malloc (sizeof (int) * 10);
- int i;
- for (i = 0; i < 10; i++)
- {
- workingnum[i] = i;
- pool_add_worker (myprocess, &workingnum[i]);
- }
- /*等待所有任务完成*/
- sleep (5);
- /*销毁线程池*/
- pool_destroy ();
- free (workingnum);
- return 0;
- }
将上述所有代码放入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
Linux平台下线程池的原理及实现的更多相关文章
- linux平台下防火墙iptables原理(转)
原文地址:http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646466.html iptables简介 netfilter/iptables( ...
- linux平台下防火墙iptables原理
iptables简单介绍 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它能够取代昂贵的商业 ...
- Thrift在Windows及Linux平台下的安装和使用示例
本文章也同时发表在个人博客Thrift在Windows及Linux平台下的安装和使用示例上. thrift介绍 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的R ...
- Java线程池的原理及几类线程池的介绍
刚刚研究了一下线程池,如果有不足之处,请大家不吝赐教,大家共同学习.共同交流. 在什么情况下使用线程池? 单个任务处理的时间比较短 将需处理的任务的数量大 使用线程池的好处: 减少在创建和销毁线程上所 ...
- Python 线程池的原理和实现及subprocess模块
最近由于项目需要一个与linux shell交互的多线程程序,需要用python实现,之前从没接触过python,这次匆匆忙忙的使用python,发现python确实语法非常简单,功能非常强大,因为自 ...
- 基于Linux/C++简单线程池的实现
我们知道Java语言对于多线程的支持十分丰富,JDK本身提供了很多性能优良的库,包括ThreadPoolExecutor和ScheduleThreadPoolExecutor等.C++11中的STL也 ...
- Java线程池实现原理及其在美团业务中的实践
本文转载自Java线程池实现原理及其在美团业务中的实践 导语 随着计算机行业的飞速发展,摩尔定律逐渐失效,多核CPU成为主流.使用多线程并行计算逐渐成为开发人员提升服务器性能的基本武器.J.U.C提供 ...
- 转:一个跨WINDOWS LINUX平台的线程类
来源:http://blog.csdn.net/dengxu11/article/details/7232681 继Windows下实现一个CThread封装类之后,这里我再实现一个跨WINDOWS ...
- Java 线程池(ThreadPoolExecutor)原理分析与使用
在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 使用线程池的好处 1.降低资源消耗 可以重复利用 ...
随机推荐
- HTML调用servlet(二)
5.修改数据 5.1编写查询条件页面 修改单条数据的时候,首先是查询出单个数据的详细信息,然后根据实际需要部分修改或者全部修改.修改之后,数据会提交到数据库,数据库中保存更新以后的数据. 查询出单条数 ...
- linux挂接U盘
挂接U盘fdisk -lDisk /dev/sdd: 131 MB, 131072000 bytes/dev/sdd1 * 1 889 127983+ b Win95 FAT32#mkdir -p / ...
- $.toJSON的使用方法
我们都会使用jQuery的ajax方法取得json数据但是我们有的时候也要使用json数据给PHP传值,这个怎么做哪? 首先去http://code.google.com/p/jquery-json/ ...
- 记centos7中的network.service启动失败
查看启动失败的服务: systemctl --failed 然后就发现了一个叫network.service 的服务启动失败了. 后来找到了原因, 是因为配置文件中没有写上网卡的硬件地址 配置文件所在 ...
- small details
1.操作数据时,一般能入库就入库,或者用xml文件,用文件操作比较复杂
- 解析php mysql 事务处理回滚操作
论坛扣币项目中,用户支付论坛币的时候如果突然断网.电脑死机.停电.等其它自然灾害时,导致本次交易没有成功(即用户的币已经扣掉了,但是服务器数据库中没有消费记录等其它情况),这种情况应该怎么样进行处理呢 ...
- 对EV-Globe5.0资源体系的简单理解
如果直接从OpenGL或DirectX底层做起的话,根本就不存在资源管理这一个思想.所谓的资源,就是说内容要从文件读取为我所用的那些文件,所以我们看到的更多的是模型.骨骼.材质.着色器. ...
- C++头文件为什么要加#ifndef #define #endif
#ifndef 在头文件中的作用 在一个大的软件工程里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时 ,就会出现大量“重定义”的错误.在头文件中实用#ifndef #de ...
- 什么是BI【转】
产品与服务 - 商务智能 目前,商业智能产品及解决方案大致可分为数据仓库产品.数据抽取产品.OLAP产品.展示产品.和集成以上几种产品的针对某个应用的整体解决方案 商业智能是什么? 简而言之, ...
- shell 加减乘除
#!/bin/basha=$1b=$2echo a+b=$(($a+$b))echo a-b=$(($a-$b))echo a*b=$(($a*$b))echo a/b=$(($a/$b))echo ...