【boost】使用装饰者模式改造boost::thread_group
在项目中使用boost::thread_group的时候遇到几个问题:
1、thread_group不提供删除全部thread列表的方法,一直使用create会是其内部列表不断增加。
2、thread_group不提供try_join_for等方法,在线程中等待时,无法调用peekmessage函数来重新激活消息队列。
由于thread_group的接口本来就比较小,因此可以直接重写,但是这个时候使用装饰者模式无疑更加方便。
namespace boost
{
class thread_group_ex
{
private:
thread_group_ex(thread_group_ex const&);
thread_group_ex& operator=(thread_group_ex const&);
public:
thread_group_ex(){}
~thread_group_ex(){} bool is_this_thread_in()
{
return m_thread_group.is_this_thread_in();
} bool is_thread_in(thread* thrd)
{
return m_thread_group.is_thread_in(thrd);
} template<typename F>
thread* create_thread(F threadfunc)
{
thread* pthread = m_thread_group.create_thread(threadfunc);
m_list_ex.push_back(pthread);
return pthread;
} void add_thread(thread* thrd)
{
m_thread_group.add_thread(thrd);
if (thrd)
{
m_list_ex.push_back(thrd);
}
} void remove_thread(thread* thrd)
{
m_thread_group.remove_thread(thrd);
m_list_ex.remove(thrd);
} void join_all()
{
m_thread_group.join_all();
}
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
void interrupt_all()
{
m_thread_group.interrupt_all();
}
#endif
size_t size() const
{
return m_thread_group.size();
}
//try join all方法
//非阻塞等待所有线程返回
void try_join_all()
{
boost::shared_lock<shared_mutex> guard(m_ex); MSG msg; for (list<thread *>::iterator it=m_list_ex.begin(); it!=m_list_ex.end(); ++it)
{
if ((*it)->joinable())
{
while (!(*it)->try_join_for(chrono::milliseconds(wait_milliseconds)))
{
PeekMessage(&msg, NULL, , , PM_NOREMOVE);
}
}
}
}
//清空列表方法
void remove_all_thread()
{
boost::shared_lock<shared_mutex> guard(m_ex);
for (list<thread *>::iterator it=m_list_ex.begin(); it!=m_list_ex.end(); ++it)
{
m_thread_group.remove_thread(*it);
delete (*it);
}
m_list_ex.clear();
} private:
const static UINT wait_milliseconds = ;
thread_group m_thread_group;
list<thread *> m_list_ex;
mutable shared_mutex m_ex;
};
}
【boost】使用装饰者模式改造boost::thread_group的更多相关文章
- 读书笔记之 - javascript 设计模式 - 装饰者模式
本章讨论的是一种为对象增添特性的技术,它并不使用创建新子类这种手段. 装饰者模式可以透明地把对象包装在具有同样接口的另一对象之中,这样一来,你可以给一些方法添加一些行为,然后将方法调用传递给原始对象. ...
- javascript设计模式——装饰者模式
前面的话 在程序开发中,许多时候都并不希望某个类天生就非常庞大,一次性包含许多职责.那么可以使用装饰者模式.装饰者模式可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.本文将 ...
- 《javascript设计模式与开发实践》阅读笔记(15)—— 装饰者模式
装饰者模式 可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.在程序开发中,许多时候都并不希望某个类天生就非常庞大,一次性包含许多职责.那么我们就可以使用装饰者模式. 代码例 ...
- 装饰者模式在JDK和Mybatis中是怎么应用的? java io包
https://mp.weixin.qq.com/s/-bj71dBylRHRqiPorOpVyg 原创: 李立敏 Java识堂 3月10日 有一个卖煎饼的店铺找上了你,希望你能给她们的店铺开发一个收 ...
- Boost.Asio 网络编程([译]Boost.Asio基本原理)
转自:https://m.w3cschool.cn/nlzbw/nlzbw-3vs825ya.html Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将 ...
- JavaScript实现AOP(面向切面编程,装饰者模式)
什么是AOP? AOP(面向切面编程)的主要作用是把一些跟核心业务逻辑模块无关的功能抽离出来,这些跟业务逻辑无关的功能通常包括日志统计.安全控制.异常处理等.把这些功能抽离出来之后, 再通过“动态织入 ...
- JS设计模式——12.装饰者模式
装饰者模式概述 本章讨论的是一种为对象添加特性的技术,她并不使用创建新子类这种手段. 装饰者模式可以用来透明的把对象包装在具有同样接口的另一个对象中.这样一来,就可以给一个方法添加一些行为,然后将方法 ...
- JavaScript设计模式-17.装饰者模式(下)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Head First设计模式——装饰者模式
前言:对于设计模式我们有时候在想是否有必要,因为实际开发中我们没有那么多闲工夫去套用这么多设计模式,也没有必要为了模式而模式. 通常这些模式会引入新的抽象层,增加代码的复杂度,但是当我们掌握了这些设计 ...
随机推荐
- java web每天定时执行任务
第一步: 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 ...
- Ubuntu 14.10下安装深度音乐客户端
很多刚从windows系统投靠到ubuntu的机油,在听音乐时不是很舒心.毕竟ubuntu软件中心的很多影音软件都是国外的朋友编写的,所以很多时候国内的朋友用着很不舒服.今天给大家推荐的是国内开发者针 ...
- VMware中第一次启动mac遇到的错误及解决方案
本文部分参考 http://blog.sina.com.cn/s/blog_938d86e90100z5ep.html 虚拟机版本:VMware-workstation-full-7.1.3-3242 ...
- JavaScript —— 局部变量和全局变量
JS的全局变量有3种声明方式: 1.Function 外 var v_myVar; 2.Function 内 v_myVar; 3.window.v_myVar window.v_myVar 全局变量 ...
- 51nod 1050 循环数组最大子段和 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1050¬iceId=13385 参考:http://blog. ...
- 《OD学hadoop》第一周0626
一.磁盘管理 Linux添加新硬盘.分区.格式化.自动挂载 http://lxsym.blog.51cto.com/1364623/321643 给Linux系统新增加一块硬盘 http://www. ...
- HDU 4006 The kth great number【优先队列】
题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数 用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了 另外这个维护队列只有k个元素的时候需要注意一下 ...
- 淘宝主搜索离线集群完成Hadoop 2
淘宝搜索离线dump集群(hadoop&hbase)2013进行了几次重大升级,本文中将这些升级的详细过程.升级中所遇到的问题以及这些问题的解决方案分享给大家.至此,淘宝主搜索离线集群完全进入 ...
- 编译安装apache+php(加常见问题解决)
[编译apache]./configure --prefix=/usr/local/lamp/httpd -with-apr=/usr/local/apr -with-apr-util=/usr/lo ...
- PHP实现站点pv,uv统计(三)
数据分析脚本如下: //error_reporting(0);date_default_timezone_set("PRC");$environment = get_cfg_var ...