BOOST 线程完全攻略 - 扩展 - 线程消息通讯
- // controlled_module_ex.hpp : controlled_module类的扩展
- // 增强线程之间消息通讯
- // 增加线程安全启动和安全关闭功能
- // 增加定时器功能
- #pragma once
- #include <boost/shared_ptr.hpp>
- #include <boost/any.hpp>
- #include "controlled_module.hpp"
- struct _command
- {
- typedef boost::shared_ptr<_command> CCmdPtr;
- unsigned int nCmd;
- boost::any anyParam;
- };
- struct _wait_command
- {
- boost::any par;
- unsigned int command;
- void * event;
- boost::shared_ptr<boost::any> resp;
- };
- class controlled_module_ex;
- struct _notify
- {
- controlled_module_ex * sender;
- int id;
- boost::any par;
- };
- #define BM_RESERVE 1000
- #define BM_RING_START BM_RESERVE+1
- #define BM_RING_STOP BM_RESERVE+2
- #define BM_RING_SETTIME BM_RESERVE+3
- #define BM_RING_SETPARENT BM_RESERVE+4
- #define BM_RING_CYCLE BM_RESERVE+5
- #define BM_RING_PROCESS BM_RESERVE+6
- #define BM_RING_PROCESSEND BM_RESERVE+7
- #define BM_RING_PROCESSFAIL BM_RESERVE+8
- #define BM_TIMER BM_RESERVE+9
- #define BM_COMMAND BM_RESERVE+10
- #define BM_NOTIFY BM_RESERVE+11
- #define BM_USER 9000
- class controlled_timer;
- class controlled_module_ex: public controlled_module
- {
- public:
- controlled_module_ex()
- {
- m_safe = false;
- }
- ~controlled_module_ex()
- {
- safestop();
- }
- public:
- template<typename T>
- bool postmessage(unsigned int nCmd, const boost::shared_ptr<T>& p)
- {
- if(this==0||!m_safe)return false;
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = nCmd;
- cmd->anyParam = p;
- m_list_command.push_back(cmd);
- return true;
- }
- boost::any execute(unsigned int command,boost::any par,int timeout=-1)
- {
- boost::shared_ptr<_wait_command> shared(new _wait_command);
- _wait_command & cmd = *shared;
- cmd.command = command;
- cmd.event = (void *)CreateEvent(0,FALSE,FALSE,0);
- cmd.par = par;
- cmd.resp = boost::shared_ptr<boost::any>(new boost::any);
- if(this->postmessage(BM_COMMAND,shared))
- {
- DWORD dw = WaitForSingleObject(cmd.event,timeout);
- CloseHandle(cmd.event);
- if(dw!=WAIT_OBJECT_0)
- return boost::any();
- else
- return *cmd.resp;
- }
- else
- {
- CloseHandle(cmd.event);
- return boost::any();
- }
- }
- void notify(_notify p)
- {
- this->postmessage(BM_NOTIFY,p);
- }
- bool postmessage(unsigned int nCmd,boost::any p)
- {
- if(this==0||!m_safe)
- return false;
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = nCmd;
- cmd->anyParam = p;
- m_list_command.push_back(cmd);
- return true;
- }
- bool postmessage(unsigned int nCmd)
- {
- if(this==0||!m_safe)
- return false;
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = nCmd;
- cmd->anyParam = 0;
- m_list_command.push_back(cmd);
- return true;
- }
- virtual bool work()
- {
- if(!getmessage())
- return false;
- else
- {
- Sleep(this->m_sleeptime);
- return true;
- }
- }
- virtual void message(const _command & cmd)
- {
- if(cmd.nCmd==BM_RING_START)
- {
- this->on_safestart();
- }
- else if(cmd.nCmd==BM_RING_STOP)
- {
- this->on_safestop();
- }
- else if(cmd.nCmd==BM_TIMER)
- {
- this->on_timer(boost::any_cast<controlled_timer*>(cmd.anyParam));
- }
- else if(cmd.nCmd==BM_COMMAND)
- {
- boost::shared_ptr<_wait_command> shared = boost::any_cast< boost::shared_ptr<_wait_command> >(cmd.anyParam);
- _wait_command & cmd = *shared;
- *cmd.resp = this->on_command(cmd.command,cmd.par);
- SetEvent((HANDLE)cmd.event);
- }
- else if(cmd.nCmd==BM_NOTIFY)
- {
- try
- {
- _notify par = boost::any_cast<_notify>(cmd.anyParam);
- this->on_notify(par);
- }
- catch(boost::bad_any_cast)
- {
- }
- }
- }
- virtual void release()
- {
- boost::mutex::scoped_lock lock(m_mutex_command);
- m_list_command.clear();
- }
- void safestart()
- {
- if(!islive())
- start();
- m_safe = true;
- m_safestart_event = (void*)CreateEvent(NULL,FALSE,FALSE,0);
- postmessage(BM_RING_START);
- ::WaitForSingleObject((HANDLE)m_safestart_event,INFINITE);
- CloseHandle(m_safestart_event);
- }
- void safestop()
- {
- if(this->islive())
- {
- m_safe = false;
- m_safestop_event = (void*)CreateEvent(NULL,FALSE,FALSE,0);
- {
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = BM_RING_STOP;
- cmd->anyParam = 0;
- m_list_command.push_back(cmd);
- }
- DWORD dw = ::WaitForSingleObject((HANDLE)m_safestop_event,3*1000);
- if(WAIT_OBJECT_0!=dw)
- {
- }
- CloseHandle(m_safestop_event);
- stop();
- }
- }
- virtual void on_timer(const controlled_timer * p){}
- virtual void on_safestart()
- {
- SetEvent(m_safestart_event);
- }
- virtual void on_safestop()
- {
- SetEvent(m_safestop_event);
- }
- virtual void on_notify(const _notify & p)
- {
- }
- protected:
- virtual boost::any on_command(const unsigned int command,const boost::any par)
- {
- return boost::any();
- }
- bool getmessage()
- {
- std::list<_command::CCmdPtr> cache;
- {
- boost::mutex::scoped_lock lock(m_mutex_command);
- while(!m_list_command.empty())
- {
- _command::CCmdPtr p = m_list_command.front();
- m_list_command.pop_front();
- cache.push_back(p);
- }
- }
- _command::CCmdPtr stop_command;
- std::list<_command::CCmdPtr>::iterator item;
- for(item = cache.begin();item!=cache.end();item++)
- {
- if((*(*item)).nCmd==BM_RING_STOP)
- {
- stop_command = *item;
- break;
- }
- }
- if(stop_command.get()==0)
- {
- while(!cache.empty())
- {
- _command::CCmdPtr p = cache.front();
- cache.pop_front();
- try
- {
- if((*p).nCmd!=BM_RING_START)
- {
- if(!this->m_safe)
- continue;
- }
- this->message(*p);
- }
- catch(boost::bad_any_cast &)
- {
- }
- }
- return true;
- }
- else
- {
- cache.clear();
- this->message(*stop_command);
- return false;
- }
- }
- private:
- void* m_safestart_event;
- void* m_safestop_event;
- bool m_safe;//在多线程,尤其牵涉到线程之间有类似socket级别关联时,当父线程safestop以后有可能会收到其他线程的postmessage,这时会引起线程死锁,这个m_safe就是解决这个问题的,当safestop以后不再接收新消息处理
- boost::mutex m_mutex_command;
- std::list<_command::CCmdPtr> m_list_command;
- };
- class controlled_timer: public controlled_module_ex
- {
- public:
- controlled_timer()
- {
- this->m_time = 0;
- this->m_parent = 0;
- this->m_step = 0;
- }
- ~controlled_timer(){
- }
- protected:
- controlled_module_ex* m_parent;
- int m_time;
- int m_step;
- public:
- void starttimer(int time,controlled_module_ex* parent)
- {
- this->safestart();
- this->postmessage(BM_RING_SETPARENT,parent);
- this->postmessage(BM_RING_SETTIME,time);
- }
- void stoptimer()
- {
- this->safestop();
- }
- public:
- virtual void on_safestop()
- {
- m_time = 0;
- controlled_module_ex::on_safestop();
- }
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_RING_SETTIME)
- {
- int time = boost::any_cast<int>(cmd.anyParam);
- this->m_time = time/this->m_sleeptime;
- this->postmessage(BM_RING_CYCLE);
- }
- else if(cmd.nCmd==BM_RING_SETPARENT)
- {
- this->m_parent = boost::any_cast<controlled_module_ex*>(cmd.anyParam);
- }
- else if(cmd.nCmd==BM_RING_CYCLE)
- {
- if(m_time>0)
- {
- if(m_step>m_time)
- {
- m_parent->postmessage(BM_TIMER,this);
- m_step=0;
- }
- m_step++;
- }
- this->postmessage(BM_RING_CYCLE);
- }
- }
- };
1.向线程PostMessage
- #include "controlled_module_ex.hpp"
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_USER+1)
- {
- cout << "get message" << endl;
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.postmessage(BM_USER+1);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
- #include "controlled_module_ex.hpp"
- struct mystruct
- {
- string a;
- int b;
- };
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_USER+1)
- {
- cout << "get integer:" << boost::any_cast<int>(cmd.anyParam) << endl;
- }
- if(cmd.nCmd==BM_USER+2)
- {
- cout << "get string:" << boost::any_cast<string>(cmd.anyParam) << endl;
- }
- if(cmd.nCmd==BM_USER+3)
- {
- mystruct par = boost::any_cast<mystruct>(cmd.anyParam);
- cout << "get mystruct:" << par.a << "," << par.b << endl;
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.postmessage(BM_USER+1,123);
- t.postmessage(BM_USER+2,string("hello world"));
- mystruct par;
- par.a = "hello world";
- par.b = 123;
- t.postmessage(BM_USER+3,par);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
3.向线程PostMessage,并传递内存块参数
- #include "controlled_module_ex.hpp"
- struct mystruct
- {
- boost::shared_ptr<char> data;
- int datalen;
- };
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_USER+1)
- {
- cout << "get sharedptr" << endl; //仅仅得到数据,得不到数据长度
- }
- if(cmd.nCmd==BM_USER+2)
- {
- mystruct par = boost::any_cast<mystruct>(cmd.anyParam);
- cout << "get sharedptr len:" << par.datalen << endl;
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.postmessage(BM_USER+1,boost::shared_ptr<char>(new char[1000]));
- mystruct par;
- par.datalen = 1000;
- par.data = boost::shared_ptr<char>(new char[par.datalen]);
- t.postmessage(BM_USER+2,par);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
3.向线程SendMessage
- #include "controlled_module_ex.hpp"
- class thdex: public controlled_module_ex
- {
- protected:
- boost::any on_command(const unsigned int command,const boost::any par)
- {
- if(command==1)
- {
- cout << "on command" << endl;
- return 0;
- }
- if(command==2)
- {
- cout << "on command,par:" << boost::any_cast<string>(par) << endl;
- return 0;
- }
- if(command==3)
- {
- return true;
- }
- else
- return controlled_module_ex::on_command(command,par);
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.execute(1,0);//等待子线程处理完成
- t.execute(2,string("hello world"));//带参数 等待子线程完成
- bool rs = boost::any_cast<bool>(t.execute(3,0));//等待子线程处理完成,并取得返回值
- cout << "get thread result:" << rs << endl;
- boost::any timeout = t.execute(4,0,1000);//等待子线程处理,超时1秒
- if(timeout.empty())
- cout << "timeout " << endl;
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
4.定时器
- #include "controlled_module_ex.hpp"
- class thdex: public controlled_module_ex
- {
- protected:
- virtual void on_timer(const controlled_timer *p)
- {
- cout << "ontimer" << endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- controlled_timer timer;
- t.safestart();
- timer.starttimer(1000,&t);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
BOOST 线程完全攻略 - 扩展 - 线程消息通讯的更多相关文章
- BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类
本文假设读者已经基本了解boost线程库的使用方法. boost是个开源工程,线程这一块也在不断完善之中,到现在这个阶段,boost::thread仅仅实现了一个完美的技术框架,但是读者在实际使用中会 ...
- BOOST 线程完全攻略 - 扩展 - 事务线程
扩展threadtimermoduleexceptionsocket 什么叫事务线程 举个例子: 我们写一个IM客户端的登录子线程,则该子线程会有这么几个事务要处理 No.1 TCP Socket物理 ...
- BOOST 线程完全攻略 - 结束语
modulethread扩展多线程破解通讯 全文介绍了3个boost::thread的扩展类,希望能给大家书写多线程代码带来便捷. thread -> controlled_module_ex ...
- 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一个 ...
- VCL线程的同步方法 Synchronize(用消息来同步)
看本文时,可以同时参考:Delphi中线程类 TThread实现多线程编程(事件.临界区.Synchronize.WaitFor……) 先说一下RTL和VCL RTL(Run-Time library ...
- boost中asio网络库多线程并发处理实现,以及asio在多线程模型中线程的调度情况和线程安全。
1.实现多线程方法: 其实就是多个线程同时调用io_service::run for (int i = 0; i != m_nThreads; ++i) { boo ...
- Chrome插件(扩展)开发全攻略
[干货]Chrome插件(扩展)开发全攻略:https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html
随机推荐
- ASP.NET中在线用户统计
统计在线用户的作用不言而喻,就是为了网站管理者可以知道当前用户的多少,然后根据用户数量来观察服务器或者程序的性能,从而可以直观的了解到网站的吸引力或者网站程序的效率.现在,我们就介绍一个简单明了的方法 ...
- hive函数总结-字符串函数
hive 查看函数: show functions; parse_url: parse_url(url, partToExtract[, key]) - extracts a part from a ...
- (原)10-folder交叉验证
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6069731.html 参考网址: https://github.com/cmusatyalab/ope ...
- [Mugeda HTML5技术教程之15]案例分析:制作移动教育课件
本文档要分析的案例是一个一氧化碳还原氧化铜的教育小课件,从中可以体会一些Mugeda API的用法和使用Mugeda动画制作移动教育课件的方法.Mugeda为移动教育领域和移动数字出版领域提供理想的教 ...
- POJ3041 二分图最大匹配
问题:POJ3041 分析: 构造二分图:令A = B = { 1, 2, ... , n }, 分别代表行号集与列号集.假如第i行第j列有一颗行星,则连接Ai与Bj, 表示必须从Ai(即第i行),B ...
- WordPress插件制作教程(三): 添加菜单的方法
上一篇编写了一个简单的插件,让大家对插件的简单制作有个了解,这一篇我们在更深一步,当我们激活插件后后台会显示菜单出来,然后通过单击菜单显示自己定义好的信息.激活之后会在WordPress后台显示一个菜 ...
- Small factorials Solved Problem code: FCTRL2
import sys def fact(n): final = n while n > 1: final *= n - 1 n -= 1 return final #逻辑严谨,不要忘了retur ...
- Taum and B'day
//自己 def main(): t = int(raw_input()) for _ in range(t): units = 0 b, w = map(int, raw_input().strip ...
- python的闭包以及闭包在设计里的意图和作用
def test(func): print func(3,4) test(lambda x,y : x+y) test(lambda x,y : x-y) 这是python的一种闭包写法,他的设计意图 ...
- [Ioi2007]Miners 矿工配餐(BZOJ1806)
[Ioi2007]Miners 矿工配餐 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 214 Solved: 128 Description 现有两 ...