【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设计模式——装饰者模式
前言:对于设计模式我们有时候在想是否有必要,因为实际开发中我们没有那么多闲工夫去套用这么多设计模式,也没有必要为了模式而模式. 通常这些模式会引入新的抽象层,增加代码的复杂度,但是当我们掌握了这些设计 ...
随机推荐
- opencv显示鼠标所在位置的rgb值
#include"highgui.h" #include"cv.h" #include"cxcore.h" #include<stdl ...
- powerdesigner 15 如何导出sql schema
PowerDesigner导出所有SQL脚本 操作:Database=>Generate Database PowerDesigner怎么导出建表sql脚本 1 按照数据库类型,切换数据库. D ...
- windows下安装python,安装框架django。
第一步: 首先下载python安装包: 第二步:安装 双击安装包,安装程序. 这里安装到C盘 文件夹命名为 python33. 正在安装......... ...
- Spring Injection with @Resource, @Autowired and @Inject
August 1st, 2011 by David Kessler Overview I’ve been asked several times to explain the difference b ...
- Android电源管理-休眠简要分析
一.开篇 1.Linux 描述的电源状态 - On(on) S0 - Working - Standb ...
- AOJ - 2224 Save your cat(最小生成树)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45524 NY在自己的花园里养了很多猫.有一天,一个巫婆在N个点设置了魔法,然 ...
- leetcode:Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...
- hadoop hadoop-0.20.2-cdh3u4升级
[hadoop@lab02 ~]$ uname -aLinux lab02 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_ ...
- 4630 no pain no game 树状数组
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你N个数,然后给你M个询问,每个询问包含一个l 一个r,问你lr 这个区间中任意两个数最 ...
- POJ 2069 Super Star
模拟退火. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...