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可以复用同一个模板实例而不会发生冲突.我们使用模板类访问底层数据,根据持久化技术 ...
随机推荐
- Oracle—RMAN备份(一)
一.RMAN备份相关概念 1.RMAN备份中表空间不需要处于backup模式下,它备份数据文件,归档日志文件,控制文件,spfile和备份集片,但不备份联机重做日志文件,临时文件和口令文件. 2.备份 ...
- 常用aliyun公共资源列表
公共DNS 223.5.5.5 223.6.6.6 源软件镜像站点 mirros.aliyun.com NTP服务器 unix like ntp1-7. ...
- 限制内容长度(CSS,jQuery)
CSS(宽度限制在100px之内,超出就会点点点) <style type="text/css"> p{width: 100px;display: inline-blo ...
- SQL Server 2005中的分区表(三):将普通表转换成分区表
在设计数据库时,经常没有考虑到表分区的问题,往往在数据表承重的负担越来越重时,才会考虑到分区方式,这时,就涉及到如何将普通表转换成分区表的问题了. 那么,如何将一个普通表转换成一个分区表 呢?说到底, ...
- excel 下载
public string CreateExcel(string SelectedBizType, string strReportDate, DropDownList ddlYQ, DropDown ...
- iOS_SN_BlueTooth( 一)蓝牙相关基础知识
原文 http://www.cocoachina.com/ios/20150915/13454.html 作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad ,ip ...
- OpenGL ES 2.0 绘制方式
OpenGL ES 中支持的绘制方式大致分3类,包括点.线段.三角形,每类中包括一种或多种具体的绘制方式. GL_POINTS 传入渲染管线的一系列顶点单独进行绘制. GL_LINES 传入渲染管 ...
- C#通过生成ini文件,记住用户关闭程序之前的选择+忽略跨线程检查
1.在类的里面添加 //写配置文件 [DllImport("kernel32")] private static extern long WritePrivateProfileSt ...
- Windows Server 2003 SP2企业版ISO下载, windows2003系统下载,2003系统下载,2003系统
Windows Server 2003 SP2 企业版ISO下载(真正企业免激活版) 此版本适合作为一个新系统来安装,也适合在虚拟机中安装 点评:Windows Server 2003 SP2 企业版 ...
- Python批量修改文本文件内容
Python批量替换文件内容,支持嵌套文件夹 import os path="./" for root,dirs,files in os.walk(path): for name ...