BOOST 线程完全攻略 - 扩展 - 事务线程
- void ThreadLogin()
- {
- try
- {
- if(fail(物理连接))
- throw;
- if(fail(登录))
- throw;
- if(fail(查询好友))
- throw;
- if(fail(更新))
- throw;
- }
- catch(exception)
- {
- }
- }
串行的逻辑用串行的代码写,不太好看,况且中途如果主线程发出取消指令,还不好处理。
- class threadLogin
- {
- void onEventConnect()
- {
- 物理连接
- }
- void onEventLogin()
- {
- 登录
- }
- void onEventQuery()
- {
- 查询
- }
- void onEventUpdate()
- {
- 更新
- }
- }
- // thread.hpp : controlled_module_ex类的扩展
- // 增强线程事务处理能力
- #pragma once
- #include "controlled_module_ex.hpp"
- class thread: public controlled_module_ex
- {
- protected:
- static const int NONE = -1;
- static const int WAITING =-2;
- static const int DONE =-3;
- static const int FAILED =-4;
- protected:
- struct process
- {
- int level;
- int status;
- int sequence;
- int trycount;
- int tryindex;
- std::string lasterror;
- double timeout;
- bool bTimeout;
- };
- process m_process;
- controlled_timer m_timer_process;
- int m_process_begin,m_process_end;
- double m_timeout_default;
- public:
- void startprocess(int process_begin,int process_end,double timeout_default=1.0,int cycle=1000)
- {
- m_process_begin = process_begin;
- m_process_end = process_end;
- m_timeout_default = timeout_default;
- m_process.level = m_process_begin;
- m_process.tryindex = 0;
- this->postmessage(BM_RING_PROCESS);
- m_timer_process.starttimer(cycle,this);
- }
- void tryagain()
- {
- if(this->m_process.level==thread::NONE)
- return;
- this->m_process.tryindex++;
- if(this->m_process.trycount>0 && this->m_process.tryindex>=this->m_process.trycount)
- {
- this->fail();
- }
- else
- this->postmessage(BM_RING_PROCESS);
- }
- void next()
- {
- if(this->m_process.level==thread::NONE)
- return;
- if(this->m_process.level>=this->m_process_end)
- {
- this->m_timer_process.stoptimer();
- this->postmessage(BM_RING_PROCESSEND);
- }
- else
- {
- this->m_process.tryindex = 0;
- this->m_process.level++;
- this->m_process.bTimeout = false;
- this->postmessage(BM_RING_PROCESS);
- }
- }
- void fail()
- {
- m_process.level = thread::NONE;
- this->m_timer_process.stoptimer();
- this->postmessage(BM_RING_PROCESSFAIL);
- }
- virtual void on_safestart()
- {
- m_process.level = thread::NONE;
- m_process.status = thread::NONE;
- m_process_begin = m_process_end = thread::NONE;
- controlled_module_ex::on_safestart();
- }
- virtual void on_safestop()
- {
- m_timer_process.stoptimer();
- controlled_module_ex::on_safestop();
- }
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_RING_PROCESS)
- {
- this->on_process();
- }
- if(cmd.nCmd==BM_RING_PROCESSEND)
- {
- this->m_process.level = thread::NONE;
- this->on_process_end();
- }
- if(cmd.nCmd==BM_RING_PROCESSFAIL)
- {
- this->m_process.level = thread::NONE;
- this->on_process_fail();
- }
- }
- virtual void on_timer(const controlled_timer * p)
- {
- if(p==this->m_timer_process)
- {
- if(this->m_process.level!=thread::NONE)
- {
- if(this->m_process.level>=this->m_process_begin && this->m_process.level<=this->m_process_end)
- {
- if(this->m_process.status==thread::NONE)
- {
- this->m_process.level = this->m_process_begin;
- m_process.tryindex = 0;
- on_process();
- }
- else if(this->m_process.status==thread::WAITING)
- {
- if(this->m_process.timeout>0)
- {
- time_t cur;
- time(&cur);
- if(difftime(cur,(time_t)this->m_process.sequence)>this->m_process.timeout)
- {
- this->m_process.bTimeout = true;
- this->tryagain();
- }
- }
- }
- else if(this->m_process.status==thread::FAILED)
- {
- this->tryagain();
- }
- else if(this->m_process.status==thread::DONE)
- {
- this->m_process.level++;
- m_process.tryindex = 0;
- this->on_process();
- }
- }
- }
- }
- }
- virtual void on_process()
- {
- time((time_t*)&m_process.sequence);
- m_process.timeout = m_timeout_default;
- m_process.status = thread::WAITING;
- m_process.trycount = -1;
- }
- virtual void on_process_end(){}
- virtual void on_process_fail(){}
- int get_sequence(){return m_process.sequence;}
- void put_timeout(double v){m_process.timeout = v;}
- void put_trycount(int v){m_process.trycount = v;}
- int get_level(){return m_process.level;}
- void put_level(int v){m_process.level=v;}
- std::string get_lasterror(){return m_process.lasterror;}
- void put_lasterror(std::string v){m_process.lasterror=v;}
- __declspec(property(put=put_trycount)) int trycount;
- __declspec(property(put=put_timeout)) double timeout;
- __declspec(property(get=get_level,put=put_level)) int level;
- __declspec(property(get=get_sequence)) int sequence;
- __declspec(property(get=get_lasterror,put=put_lasterror)) std::string lasterror;
- };
虚拟函数thread::on_process()处理各种事务事件
- #define PROCESS_1 1
- #define PROCESS_2 2
- #define PROCESS_3 3
- class thdex: public thread
- {
- public:
- virtual void on_process()
- {
- thread::on_process();
- if(this->level==PROCESS_1)
- {
- cout << "work on process 1..." << endl;
- Sleep(100);
- cout << "process 1 done." << endl;
- this->next();
- }
- else if(this->level==PROCESS_2)
- {
- cout << "work on process 2..." << endl;
- this->timeout = -1;
- if(IDNO==::MessageBox(0,"are your want continue?","ask",MB_ICONQUESTION|MB_YESNO))
- {
- this->lasterror = "canceled by user";
- this->fail();
- }
- else
- {
- Sleep(100);
- cout << "process 2 done." << endl;
- this->next();
- }
- }
- else if(this->level==PROCESS_3)
- {
- cout << "work on process 3..." << endl;
- Sleep(100);
- cout << "process 3 done." << endl;
- this->next();
- }
- }
- virtual void on_process_fail()
- {
- cout << this->lasterror << endl;
- }
- virtual void on_process_end()
- {
- cout << "all process done." << endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- thdex t;
- t.safestart();
- t.startprocess(PROCESS_1,PROCESS_3);
- char buf[10];
- gets_s(buf,sizeof buf);
- t.safestop();
- return 0;
- }
BOOST 线程完全攻略 - 扩展 - 事务线程的更多相关文章
- BOOST 线程完全攻略 - 扩展 - 线程消息通讯
// controlled_module_ex.hpp : controlled_module类的扩展 // 增强线程之间消息通讯 // 增加线程安全启动和安全关闭功能 // 增加定时器功能 #p ...
- BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类
本文假设读者已经基本了解boost线程库的使用方法. boost是个开源工程,线程这一块也在不断完善之中,到现在这个阶段,boost::thread仅仅实现了一个完美的技术框架,但是读者在实际使用中会 ...
- 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 ...
- Java视频扩展知识 线程池的了解
Java视频扩展知识 线程池的了解 1.简单介绍: Jdk1.5之后加入了java.util.concurrent包,这个包中主要介绍java中线程以及线程池的使用.为我们在开发中处理线程的 ...
- Chrome插件(扩展)开发全攻略
[干货]Chrome插件(扩展)开发全攻略:https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html
- 线程本地变更,即ThreadLocal-->Spring事务管理
我们知道Spring通过各种模板类降低了开发者使用各种数据持久技术的难度.这些模板类都是线程安全的,也就是说,多个DAO可以复用同一个模板实例而不会发生冲突.我们使用模板类访问底层数据,根据持久化技术 ...
随机推荐
- Hacker(16)----防范端口扫描与嗅探
端口扫描与嗅探都是黑客常用的招数,其目的是定位目标计算机和窃取隐私信息.为确保自己计算机的安全,用户需要掌握防范嗅探与端口扫描的常见措施,保障个人隐私信息安全. 一.掌握防范端口扫描的常用措施 防范端 ...
- css_day7
- ios按钮点击时的灰色框
a,button,input,textarea{-webkit-tap-highlight-color: rgba(0,0,0,0;)}
- 无法从带有索引像素格式的图像创建graphics对象(转)
大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...
- js获取get值
//获取get值 function getPar(par) { //获取当前URL var local_url = document.location.href; //获取要取得的get参数位置 va ...
- REST 相关
REST 相关 REST:Representational State Transfer,表现层状态转化(出现在阮一峰的博客 理解RESTful架构 中,但是,很明显,Representational ...
- CocoaPod安装
http://www.360doc.com/content/14/0309/10/11029609_358970353.shtml http://www.bubuko.com/infodetail-4 ...
- iOS的触摸事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称其为@''响应者对象''UIApplication,UIViewController,UIView都 ...
- 常见的DoDataExchange什么意思
该函数中的代码是由ClassWizard自动加入的.DoDataExchange只有一个参数,即一个CDataExchange对象的指针pDX.在该函数中调用了DDX函数来完成数据交换,调用DDV函数 ...
- macbook安装mysql
一.官网下载dmg文件 二.双击安装dmg文件,一路next: 三.