一个C++基于boost简单实现的线程池
xl_blocking_queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#ifndef SRC_COMMON_BLOCKING_QUEUE_H_ #define SRC_COMMON_BLOCKING_QUEUE_H_ #include <boost/thread.hpp> #include <boost/noncopyable.hpp> #include <queue> template < typename T> class xl_blocking_queue :boost::noncopyable { public : xl_blocking_queue() :mutex_(), queue_(), cond_() { } ~xl_blocking_queue(){} void put( const T& func) { boost::unique_lock<boost::mutex> lock(mutex_); queue_.push(func); cond_.notify_all(); } T get() { boost::unique_lock<boost::mutex> lock(mutex_); if (queue_.size() == 0) { cond_.wait(lock); } T front(queue_.front()); queue_.pop(); return front; } unsigned size() { return queue_.size(); } void notify_all() { cond_.notify_all(); } private : std::queue<T> queue_; boost::condition_variable_any cond_; boost::mutex mutex_; }; #endif |
xl_thread_pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<strong>#ifndef SRC_COMMON_THREAD_POOL_H_ #define SRC_COMMON_THREAD_POOL_H_ #include <boost/thread.hpp> #include <boost/shared_ptr.hpp> #include <boost/noncopyable.hpp> #include <vector> #include "xl_blocking_queue.h" typedef boost::function< void ( void )> thread_do_func; class xl_thread_pool :boost::noncopyable { public : xl_thread_pool( int thread_num) :num_(thread_num), run_( false ) { } ~xl_thread_pool() { if (run_) { stop(); } } void start() { if (num_ <= 0) return ; int i = 0; run_ = true ; for (i=0;i<num_;i++) { boost::shared_ptr<boost:: thread > thread ( new boost:: thread (boost::BOOST_BIND(&xl_thread_pool::run, this ))); thread_arr_.push_back( thread ); } } void stop() { run_ = false ; queue_.notify_all(); } void post( const thread_do_func& task) { if (thread_arr_.size() == 0) { task(); } else { queue_.put(task); } } private : xl_blocking_queue<thread_do_func> queue_; std::vector<boost::shared_ptr<boost:: thread > > thread_arr_; int num_; bool run_; void run() { while (run_) { thread_do_func task = queue_.get(); task(); } } }; #endif |
一个C++基于boost简单实现的线程池的更多相关文章
- 基于队列queue实现的线程池
本文通过文章同步功能推送至博客园,显示排版可能会有所错误,请见谅! 写在前文:在Python中给多进程提供了进程池类,对于线程,Python2并没有直接提供线程池类(Python3中提供了线程池功能) ...
- 一个简单的python线程池框架
初学python,实现了一个简单的线程池框架,线程池中除Wokers(工作线程)外,还单独创建了一个日志线程,用于日志的输出.线程间采用Queue方式进行通信. 代码如下:(不足之处,还请高手指正) ...
- 一个简单的linux线程池(转-wangchenxicool)
线程池:简单地说,线程池 就是预先创建好一批线程,方便.快速地处理收到的业务.比起传统的到来一个任务,即时创建一个线程来处理,节省了线程的创建和回收的开销,响应更快,效率更高. 在linux中,使用的 ...
- 自定义简单版本python线程池
python未提供线程池模块,在python3上用threading和queue模块自定义简单线程池,代码如下: #用threading queue 做线程池 import queue import ...
- concurrent.futures模块简单介绍(线程池,进程池)
一.基类Executor Executor类是ThreadPoolExecutor 和ProcessPoolExecutor 的基类.它为我们提供了如下方法: submit(fn, *args, ** ...
- Linux杂谈: 实现一种简单实用的线程池(C语言)
基本功能 1. 实现一个线程的队列,队列中的线程启动后不再释放: 2. 没有任务执行时,线程处于pending状态,等待唤醒,不占cpu: 3. 当有任务需要执行时,从线程队列中取出一个线程执行任务: ...
- 简单实现java线程池
使用多线程以及线程池的意义无需多说,要想掌握线程池,最好的方法还是自己手动去实现. 一.实现思路 (网络盗图) 二.实现代码 1.线程池类 package com.ty.thread; im ...
- 基于C++11实现的线程池
1.C++11中引入了lambada表达式,很好的支持异步编程 2.C++11中引入了std::thread,可以很方便的构建线程,更方便的可移植特性 3.C++11中引入了std::mutex,可以 ...
- 基于Linux/C++简单线程池的实现
我们知道Java语言对于多线程的支持十分丰富,JDK本身提供了很多性能优良的库,包括ThreadPoolExecutor和ScheduleThreadPoolExecutor等.C++11中的STL也 ...
随机推荐
- Navicat:cant create OCI environment.
一直在使用 Navicat ,这是一个数据库客户端软件,能连接多种不同类型的数据库,给我们的日常的工作带来了不少的便捷. 最近,我在电脑上安装了oracle的客户端ODTwihtODAC121012, ...
- codesmith的使用
新建一个C#模版. model类的模版代码如下: <%-- Name: 模型层代码生成模版 Author: XX Description: 根据数据库的内容生成模型层代码 Version: V1 ...
- 使用UILocalNotification给App添加本地消息通知
使用过的代码,直接贴上 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification ...
- PHP XML Expat 解析器
PHP XML Expat 解析器 内建的 Expat 解析器使在 PHP 中处理 XML 文档成为可能. XML 是什么? XML 用于描述数据,其焦点是数据是什么.XML 文件描述了数据的结构. ...
- HTTP Content-type 对照表
文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流) application/octet-stream .tif ...
- Python----定义
变量的定义: 变量第一次出现不是声明类型就是赋初值,才能后续使用. 函数的定义: ''' 函数的返回值不用声明类型 函数参数值最好赋一个类型值,例如整型赋值0,列表[] 函数名后面必须跟: ''' d ...
- 【转】Qt之模型/视图
[本文转自]http://blog.sina.com.cn/s/blog_a6fb6cc90101hh20.html 作者: 一去丶二三里 关于Qt中MVC的介绍与使用,助手中有一节模型/视图编程 ...
- MySQL如何执行关联查询
MySQL中‘关联(join)’ 一词包含的意义比一般意义上理解的要更广泛.总的来说,MySQL认为任何一个查询都是一次‘关联’ --并不仅仅是一个查询需要到两个表的匹配才叫关联,索引在MySQL中, ...
- WDCP控制面板安装卸载
安装 安装源码 WDCP提供两种安装模式,一种是源码安装,一种是RPM包安装,众所周知,源码安装虽然安装时要比RPM包安装繁琐,但是使用上要节省内存.所以我这里主要介绍源码安装. 首先是下载安装源代码 ...
- ubuntu 14.下 netbeans 自体锯齿 消除
Ubuntu下NetBeans消除字体锯齿的方法 在netbeans.conf 文件的netbeans_default_options的最后添加 -J-Dswing.aatext=true -J-Da ...