BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类
- // janitor.hpp : 安全执行类库
- //
- #pragma once
- #include <list>
- template <class T>
- class RefHolder
- {
- T& ref_;
- public:
- RefHolder(T& ref) : ref_(ref) {}
- operator T& () const
- {
- return ref_;
- }
- private:
- // Disable assignment - not implemented
- RefHolder& operator=(const RefHolder&);
- };
- template <class T>
- inline RefHolder<T> ByRef(T& t)
- {
- return RefHolder<T>(t);
- }
- class ScopeGuardImplBase
- {
- ScopeGuardImplBase& operator =(const ScopeGuardImplBase&);
- protected:
- ~ScopeGuardImplBase()
- {
- }
- ScopeGuardImplBase(const ScopeGuardImplBase& other) throw()
- : dismissed_(other.dismissed_)
- {
- other.Dismiss();
- }
- template <typename J>
- static void SafeExecute(J& j) throw()
- {
- if (!j.dismissed_)
- try
- {
- j.Execute();
- }
- catch(...)
- {
- }
- }
- mutable bool dismissed_;
- public:
- ScopeGuardImplBase() throw() : dismissed_(false)
- {
- }
- void Dismiss() const throw()
- {
- dismissed_ = true;
- }
- };
- typedef const ScopeGuardImplBase& ScopeGuard;
- template <typename F>
- class ScopeGuardImpl0 : public ScopeGuardImplBase
- {
- public:
- static ScopeGuardImpl0<F> MakeGuard(F fun)
- {
- return ScopeGuardImpl0<F>(fun);
- }
- ~ScopeGuardImpl0() throw()
- {
- SafeExecute(*this);
- }
- void Execute()
- {
- fun_();
- }
- protected:
- ScopeGuardImpl0(F fun) : fun_(fun)
- {
- }
- F fun_;
- };
- template <typename F>
- inline ScopeGuardImpl0<F> MakeGuard(F fun)
- {
- return ScopeGuardImpl0<F>::MakeGuard(fun);
- }
- template <typename F, typename P1>
- class ScopeGuardImpl1 : public ScopeGuardImplBase
- {
- public:
- static ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
- {
- return ScopeGuardImpl1<F, P1>(fun, p1);
- }
- ~ScopeGuardImpl1() throw()
- {
- SafeExecute(*this);
- }
- void Execute()
- {
- fun_(p1_);
- }
- protected:
- ScopeGuardImpl1(F fun, P1 p1) : fun_(fun), p1_(p1)
- {
- }
- F fun_;
- const P1 p1_;
- };
- template <typename F, typename P1>
- inline ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
- {
- return ScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
- }
- template <typename F, typename P1, typename P2>
- class ScopeGuardImpl2: public ScopeGuardImplBase
- {
- public:
- static ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
- {
- return ScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
- }
- ~ScopeGuardImpl2() throw()
- {
- SafeExecute(*this);
- }
- void Execute()
- {
- fun_(p1_, p2_);
- }
- protected:
- ScopeGuardImpl2(F fun, P1 p1, P2 p2) : fun_(fun), p1_(p1), p2_(p2)
- {
- }
- F fun_;
- const P1 p1_;
- const P2 p2_;
- };
- template <typename F, typename P1, typename P2>
- inline ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
- {
- return ScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
- }
- template <typename F, typename P1, typename P2, typename P3>
- class ScopeGuardImpl3 : public ScopeGuardImplBase
- {
- public:
- static ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
- {
- return ScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3);
- }
- ~ScopeGuardImpl3() throw()
- {
- SafeExecute(*this);
- }
- void Execute()
- {
- fun_(p1_, p2_, p3_);
- }
- protected:
- ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3) : fun_(fun), p1_(p1), p2_(p2), p3_(p3)
- {
- }
- F fun_;
- const P1 p1_;
- const P2 p2_;
- const P3 p3_;
- };
- template <typename F, typename P1, typename P2, typename P3>
- inline ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
- {
- return ScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3);
- }
- //************************************************************
- template <class Obj, typename MemFun>
- class ObjScopeGuardImpl0 : public ScopeGuardImplBase
- {
- public:
- static ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun)
- {
- return ObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
- }
- ~ObjScopeGuardImpl0() throw()
- {
- SafeExecute(*this);
- }
- void Execute()
- {
- (obj_.*memFun_)();
- }
- protected:
- ObjScopeGuardImpl0(Obj& obj, MemFun memFun)
- : obj_(obj), memFun_(memFun) {}
- Obj& obj_;
- MemFun memFun_;
- };
- template <class Obj, typename MemFun>
- inline ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun)
- {
- return ObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun);
- }
- template <class Obj, typename MemFun, typename P1>
- class ObjScopeGuardImpl1 : public ScopeGuardImplBase
- {
- public:
- static ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
- {
- return ObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
- }
- ~ObjScopeGuardImpl1() throw()
- {
- SafeExecute(*this);
- }
- void Execute()
- {
- (obj_.*memFun_)(p1_);
- }
- protected:
- ObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1)
- : obj_(obj), memFun_(memFun), p1_(p1) {}
- Obj& obj_;
- MemFun memFun_;
- const P1 p1_;
- };
- template <class Obj, typename MemFun, typename P1>
- inline ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
- {
- return ObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1);
- }
- template <class Obj, typename MemFun, typename P1, typename P2>
- class ObjScopeGuardImpl2 : public ScopeGuardImplBase
- {
- public:
- static ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
- {
- return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
- }
- ~ObjScopeGuardImpl2() throw()
- {
- SafeExecute(*this);
- }
- void Execute()
- {
- (obj_.*memFun_)(p1_, p2_);
- }
- protected:
- ObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2)
- : obj_(obj), memFun_(memFun), p1_(p1), p2_(p2) {}
- Obj& obj_;
- MemFun memFun_;
- const P1 p1_;
- const P2 p2_;
- };
- template <class Obj, typename MemFun, typename P1, typename P2>
- inline ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
- {
- return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>::MakeObjGuard(obj, memFun, p1, p2);
- }
- #define CONCATENATE_DIRECT(s1, s2) s1##s2
- #define CONCATENATE(s1, s2) CONCATENATE_DIRECT(s1, s2)
- #define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __LINE__)
- #define ON_BLOCK_EXIT ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = MakeGuard
- #define ON_BLOCK_EXIT_OBJ ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = MakeObjGuard
- //////////////////////////////////////////////////////////////////////////////////////////
- // janitor
- struct ICmd_
- {
- virtual void Dismiss() const throw() = 0;
- virtual ~ICmd_() throw() {}
- };
- template<typename T>
- class CmdAdaptor : public ICmd_, protected T
- {
- public:
- template<typename Fun>
- CmdAdaptor(Fun fun) : T(fun) {}
- template<typename Fun, typename P1>
- CmdAdaptor(Fun fun, P1 p1) : T(fun, p1) {}
- template<typename Fun, typename P1, typename P2>
- CmdAdaptor(Fun fun, P1 p1, P2 p2) : T(fun, p1, p2) {}
- template<typename Fun, typename P1, typename P2, typename P3>
- CmdAdaptor(Fun fun, P1 p1, P2 p2, P3 p3) : T(fun, p1, p2, p3) {}
- void Dismiss() const throw()
- {
- T::Dismiss();
- }
- };
- class Janitor
- {
- public:
- Janitor() throw() {}
- template <typename F>
- Janitor(F pFun) : spCmd_(
- new CmdAdaptor<ScopeGuardImpl0<F> >(pFun)) {}
- template <typename F, typename P1>
- Janitor(F pFun, P1 p1) : spCmd_(
- new CmdAdaptor<ScopeGuardImpl1<F, P1> >(pFun, p1)) {}
- template <typename F, typename P1, typename P2>
- Janitor(F pFun, P1 p1, P2 p2) : spCmd_(
- new CmdAdaptor<ScopeGuardImpl2<F, P1, P2> >(pFun, p1, p2)) {}
- template <typename F, typename P1, typename P2, typename P3>
- Janitor(F pFun, P1 p1, P2 p2, P3 p3) : spCmd_(
- new CmdAdaptor<ScopeGuardImpl3<F, P1, P2, P3> >(pFun, p1, p2, p3)) {}
- Janitor(const Janitor& other) throw() : spCmd_(other.spCmd_) {} //VC++, Comeau need it!
- Janitor& operator =(const Janitor& other) throw()
- {
- if (spCmd_.get())
- spCmd_->Dismiss();
- spCmd_ = other.spCmd_;
- return *this;
- }
- void Dismiss() const throw()
- {
- spCmd_->Dismiss();
- }
- protected:
- mutable std::auto_ptr<ICmd_> spCmd_;
- };
- template<typename T>
- class ObjCmdAdaptor : public ICmd_, protected T
- {
- public:
- template<typename Obj, typename MemFun>
- ObjCmdAdaptor(Obj& obj, MemFun memFun) : T(obj, memFun) {}
- template<typename Obj, typename MemFun, typename P1>
- ObjCmdAdaptor(Obj& obj, MemFun memFun, P1 p1) : T(obj, memFun, p1) {}
- template<typename Obj, typename MemFun, typename P1, typename P2>
- ObjCmdAdaptor(Obj& obj, MemFun memFun, P1 p1, P2 p2) : T(obj, memFun, p1, p2) {}
- void Dismiss() const throw()
- {
- T::Dismiss();
- }
- };
- class ObjJanitor : protected Janitor
- {
- public:
- using Janitor::Dismiss;
- ObjJanitor() throw() {}
- template <typename Obj, typename MemFun>
- ObjJanitor(Obj& obj, MemFun memFun)
- {
- std::auto_ptr<ICmd_> spTmp(
- new ObjCmdAdaptor<ObjScopeGuardImpl0<Obj, MemFun> >(obj, memFun));
- spCmd_ = spTmp;
- }
- template <typename Obj, typename MemFun, typename P1>
- ObjJanitor(Obj& obj, MemFun memFun, P1 p1)
- {
- std::auto_ptr<ICmd_> spTmp(
- new ObjCmdAdaptor<ObjScopeGuardImpl1<Obj, MemFun, P1> >(obj, memFun, p1));
- spCmd_ = spTmp;
- }
- template <typename Obj, typename MemFun, typename P1, typename P2>
- ObjJanitor(Obj& obj, MemFun memFun, P1 p1, P2 p2)
- {
- std::auto_ptr<ICmd_> spTmp(
- new ObjCmdAdaptor<ObjScopeGuardImpl2<Obj, MemFun, P1, P2> >(obj, memFun, p1, p2));
- spCmd_ = spTmp;
- }
- };
使用范例
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- #include "janitor.hpp"
- class class1
- {
- public:
- class1()
- {
- }
- ~class1()
- {
- }
- public:
- void test()
- {
- ObjJanitor ja(*this,&class1::testJanitor);
- }
- void testJanitor()
- {
- cout << "hello world" << endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- class1 c;
- c.test();
- return 0;
- }
二.controlled_module 可被关闭的线程类
- // controlled_module.hpp : 可主动关闭的boost线程类
- //
- #pragma once
- #include <boost/utility.hpp>
- #include <boost/thread/condition.hpp>
- #include <boost/thread/mutex.hpp>
- #include <boost/thread/thread.hpp>
- #include <boost/bind.hpp>
- #include "janitor.hpp"
- class controlled_module_implement : boost::noncopyable {
- public:
- controlled_module_implement() :active_(false),command_exit_(false){}
- boost::condition module_is_exit;
- boost::mutex monitor;
- void active(bool ac)
- {boost::mutex::scoped_lock lk(monitor); if(!(active_=ac))module_is_exit.notify_all();else command_exit_=false;}
- bool command_exit(){boost::mutex::scoped_lock lk(monitor); return command_exit_;}
- bool active_,command_exit_;
- };
- class controlled_module : boost::noncopyable {
- public:
- virtual void run()
- {
- ObjJanitor janitor(*impl_,&controlled_module_implement::active,false);
- impl_->active(true);
- {
- ObjJanitor janitor(*this,&controlled_module::release);
- if(this->initialize())
- {
- m_live = true;
- SetEvent(m_event_init);
- while(!impl_->command_exit() && this->islive() && this->work())
- {
- }
- }
- else
- {
- m_live = false;
- SetEvent(m_event_init);
- }
- }
- }
- bool exit(unsigned long sec=0)
- {
- boost::mutex::scoped_lock lk(impl_->monitor);
- impl_->command_exit_ = true;
- while(impl_->active_)
- {
- if(sec)
- {
- boost::xtime xt;
- boost::xtime_get(&xt, boost::TIME_UTC);
- xt.sec += sec;
- if(!impl_->module_is_exit.timed_wait(lk,xt))
- return false;
- }
- else
- impl_->module_is_exit.wait(lk);
- }
- return true;
- }
- protected:
- controlled_module()
- :impl_(new controlled_module_implement)
- ,m_live(false)
- ,m_event_init(0)
- ,m_sleeptime(10)
- {
- }
- virtual ~controlled_module()
- {
- if(m_live)
- stop();
- delete impl_;
- }
- private:
- virtual bool initialize(){return true;}
- virtual void release(){}
- protected:
- virtual bool work()
- {
- Sleep(this->m_sleeptime);
- return true;
- }
- int m_sleeptime;
- private:
- bool m_live;
- void * m_event_init;
- controlled_module_implement* impl_;
- public:
- bool start()
- {
- m_event_init = CreateEvent(NULL,FALSE,FALSE,"");
- boost::thread thd(boost::bind(&controlled_module::run,this));
- ::WaitForSingleObject(m_event_init,INFINITE);
- CloseHandle(m_event_init);
- m_event_init = 0;
- return m_live;
- }
- void stop()
- {
- m_live = false;
- exit(1);
- }
- bool islive(){return m_live;}
- void die()
- {
- m_live = false;
- SetEvent(m_event_init);
- }
- void setsleeptime(int n)
- {
- m_sleeptime = n;
- }
- };
- //controlled_module demo
- #include "controlled_module.hpp"
- class thd: public controlled_module
- {
- public:
- virtual bool initialize()
- {
- cout << "thd init" << endl;
- return true;
- }
- virtual void release()
- {
- cout << "thd release" << endl;
- }
- virtual bool work()
- {
- //your work........
- return controlled_module::work();
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thd t;
- t.start();
- char buf[10];
- gets_s(buf,sizeof buf);
- t.stop();
- return 0;
- }
thd线程在启动以后,将循环执行work()函数,直到线程退出
BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类的更多相关文章
- BOOST 线程完全攻略 - 扩展 - 线程消息通讯
// controlled_module_ex.hpp : controlled_module类的扩展 // 增强线程之间消息通讯 // 增加线程安全启动和安全关闭功能 // 增加定时器功能 #p ...
- BOOST 线程完全攻略 - 扩展 - 事务线程
扩展threadtimermoduleexceptionsocket 什么叫事务线程 举个例子: 我们写一个IM客户端的登录子线程,则该子线程会有这么几个事务要处理 No.1 TCP Socket物理 ...
- BOOST 线程完全攻略 - 基础篇
http://blog.csdn.net/iamnieo/article/details/2908621 2008-09-10 12:48 9202人阅读 评论(3) 收藏 举报 thread多线程l ...
- BOOST 线程完全攻略
1 创建线程 首先看看boost::thread的构造函数吧,boost::thread有两个构造函数: (1)thread():构造一个表示当前执行线程的线程对象: (2)explicit thre ...
- 【C/C++】BOOST 线程完全攻略 - 基础篇
C++多线程开发是一个复杂的事情,mfc下提供了CWinThread类,和AfxBeginThread等等函数,但是在使用中会遇到很多麻烦事情,例如线程之间参数传递的问题,我们一般都是把参数new一个 ...
- BOOST 线程完全攻略 - 结束语
modulethread扩展多线程破解通讯 全文介绍了3个boost::thread的扩展类,希望能给大家书写多线程代码带来便捷. thread -> controlled_module_ex ...
- Chrome插件(扩展)开发全攻略
[干货]Chrome插件(扩展)开发全攻略:https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html
- 零基础攻略!如何使用kubectl和HPA扩展Kubernetes应用程序
现如今,Kubernetes已经完全改变了软件开发方式.Kubernetes作为一个管理容器化工作负载及服务的开源平台,其拥有可移植.可扩展的特性,并促进了声明式配置和自动化,同时它还证明了自己是管理 ...
- Linux shell 脚本攻略之根据扩展名切分文件名
摘自:<Linux shell 脚本攻略>Page61-62
随机推荐
- Node.js 之 express 入门 ejs include公共部分
1. 直接进入express安装 因为之前有一篇文章我已经讲过怎么安装node了 而网上的教程也是非常多.所有直接进入到express.教程简陋 由于我比较笨 所有只要写到我自己明白就行. 这里有个教 ...
- oracle SQL语句练习MERGE、模糊查询、排序、
Oracle支持的SQL指令可分为数据操作语言语句.数据定义语言语句.事务控制语句.会话控制语句等几种类型:1.数据操作语言语句数据操作语言语句(Data manipulation language, ...
- 编译U-boot时,make[1]: *** 没有规则可以创建mkimage.o”
执行完make smdk2440_config 对Uboot重行编译怎么会出现这样的错误 make[1]: Entering directory `/home/win/S3-ARM/Part4/ubo ...
- css-选择器-优先级
<!DOCTYPE html>CSS选择器优先级相同优先级下,位置在文档后面的覆盖前面的不同优先级下,越能具体描述元素的css选择器,优先级越高 具体情况:HTML元素继承父级元素的CSS ...
- 试用Let's encrypt
终于等到 https://letsencrypt.org beta了,马上下载试用,发现过程超简单. 1.首先需要下载letsencrypt的客户端,官方给的介绍是 The Let’s Encrypt ...
- "ORA-00942: 表或视图不存在 "的原因和解决方法[转]
采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或视图不存在 ”,很是郁闷,这个问题以前出现过,当初解决了,但因好久没有使用,这次竟然 ...
- Java 装箱、拆箱 包装器
先说java的基本数据类型.java基本数据类型:byte.short.int.long.float.double.char.boolean 基本数据类型的自动装箱(autoboxing).拆箱(un ...
- Js与flash交互:在html页面中用js与MyReport插件交互
Html页面与flash的加载 如下图,flash是html页面的一个插件节点. js与flash进行交互,首先要处理好html页面和swf的加载问题. Swf调用外部js方法,要确保js方法已存在, ...
- mysql索引之普通索引
1,普通索引的创建 普通索引可以在建表的时候创建 sql : create table temp2(id int(10) not null auto_increment ,title varchar( ...
- centos mysql 数据存储目录安装位置
rpm -ql mysql查看安装位置 MYSQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步: 1.home目录下建立data目录 c ...