在项目中使用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的更多相关文章

  1. 读书笔记之 - javascript 设计模式 - 装饰者模式

    本章讨论的是一种为对象增添特性的技术,它并不使用创建新子类这种手段. 装饰者模式可以透明地把对象包装在具有同样接口的另一对象之中,这样一来,你可以给一些方法添加一些行为,然后将方法调用传递给原始对象. ...

  2. javascript设计模式——装饰者模式

    前面的话 在程序开发中,许多时候都并不希望某个类天生就非常庞大,一次性包含许多职责.那么可以使用装饰者模式.装饰者模式可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.本文将 ...

  3. 《javascript设计模式与开发实践》阅读笔记(15)—— 装饰者模式

    装饰者模式 可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.在程序开发中,许多时候都并不希望某个类天生就非常庞大,一次性包含许多职责.那么我们就可以使用装饰者模式. 代码例 ...

  4. 装饰者模式在JDK和Mybatis中是怎么应用的? java io包

    https://mp.weixin.qq.com/s/-bj71dBylRHRqiPorOpVyg 原创: 李立敏 Java识堂 3月10日 有一个卖煎饼的店铺找上了你,希望你能给她们的店铺开发一个收 ...

  5. Boost.Asio 网络编程([译]Boost.Asio基本原理)

    转自:https://m.w3cschool.cn/nlzbw/nlzbw-3vs825ya.html Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将 ...

  6. JavaScript实现AOP(面向切面编程,装饰者模式)

    什么是AOP? AOP(面向切面编程)的主要作用是把一些跟核心业务逻辑模块无关的功能抽离出来,这些跟业务逻辑无关的功能通常包括日志统计.安全控制.异常处理等.把这些功能抽离出来之后, 再通过“动态织入 ...

  7. JS设计模式——12.装饰者模式

    装饰者模式概述 本章讨论的是一种为对象添加特性的技术,她并不使用创建新子类这种手段. 装饰者模式可以用来透明的把对象包装在具有同样接口的另一个对象中.这样一来,就可以给一个方法添加一些行为,然后将方法 ...

  8. JavaScript设计模式-17.装饰者模式(下)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Head First设计模式——装饰者模式

    前言:对于设计模式我们有时候在想是否有必要,因为实际开发中我们没有那么多闲工夫去套用这么多设计模式,也没有必要为了模式而模式. 通常这些模式会引入新的抽象层,增加代码的复杂度,但是当我们掌握了这些设计 ...

随机推荐

  1. 大型web系统架构详解

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) 动态应用,是相对于网站静态内 ...

  2. ACdream 1726 A Math game (dfs+二分)

    http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...

  3. Html 修改placeholder的颜色属性css样式

    项目需求需要修改文本框的placeholder 的文本颜色, 百度下, 备忘,我使用的是这种方法, ::-webkit-input-placeholder { /* WebKit browsers * ...

  4. Codeforces Round #362 (Div. 2) A.B.C

    A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. 1430. Crime and Punishment(枚举)

    1430 即使是枚举 也是有一定技术含量的 对于ax+by = n: 枚举max(a,b)中的系数 这样可以确定另一个 但问题是如何确定上限 假设max(a,b) = a,很显然是不会超n/a的 但这 ...

  6. c#快捷键大全

    转发:http://zhidao.baidu.com/question/444655283 直接贴出来吧(关于VS的): 快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 ...

  7. UVa 409 Excuses, Excuses!

    哈哈,虽然是一道字符串水题,可是拿到一个1A还是很开心的! 题意就是给一些keywords(子串)和Excuse(母串),然后输出包含keywords最多的Excuse,如果相等的话,按任意顺序全部输 ...

  8. UVa (一道比较复杂的广搜) 816 Abbott’s Revenge

    题意: 给出一个迷宫,在迷宫的节点处,面向某个方向只能向给定的方向转弯.给出起点和终点输出迷宫的最短路径,这里指的是刚刚离开起点的时刻,所以即使起点和终点重合路径也非空. 分析: 用三个变量来表示状态 ...

  9. [转] POJ计算几何

    转自:http://blog.csdn.net/tyger/article/details/4480029 计算几何题的特点与做题要领:1.大部分不会很难,少部分题目思路很巧妙2.做计算几何题目,模板 ...

  10. phpstorm10.0.1和webstorm11注册

    webstorm11 for win 注册时选择“License server”输入“http://15.idea.lanyus.com/”点击“OK” phpstorm10.0.1 for win ...