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可以复用同一个模板实例而不会发生冲突.我们使用模板类访问底层数据,根据持久化技术 ...
随机推荐
- LR翻页脚本并在每页实现业务操作
性能需求:在列表中删除后有记录,或对列表中的每条记录进行操作(如点击每条记录的“单号”进入订单详情页面,或在列表中对每条记录进行“启用”.“停止”操作) 举例:Vuser脚本模拟用户在订单列表中点击每 ...
- verilog 实现加法器
半加器 如果不考虑来自低位的进位将两个1二进制数相加,称为半加. 实现半加运算的逻辑电路称为半加器. 真值表 逻辑表达式和 \begin{align}\notag s = a{b}' + {a}'b ...
- Mac_配置adb环境变量
1.打开终端Terminal. 2.进入HOME目录:命令行输入 echo(这之间有一个空格) $HOME 3.创建.bash_profile文件:命令行输入 touch(这之间有一个空格).bash ...
- NSArray使用小结
http://blog.csdn.net/ms2146/article/details/8654263
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- c++11-bind的用法
bind函数 在c++11之前,要绑定某个函数.函数对象或者成员函数的不同参数值需要用到不同的转换器,如bind1st.bind2nd.fun_ptr.mem_fun和mem_fun_ref等.在c+ ...
- html5 百分比计算
这几天一直在看html5,看到了百分比的计算公式:目标元素的尺寸/上下文元素的尺寸=百分比尺寸.看到这个公式,有点懂,但是有不明白.对于目标元素很容易理解,但是对于上下文元素就不是很好理解了.试了一些 ...
- 【6】python核心编程 第九章-文件和输入输出
1.文件对象的内建方法列表 文件对象的方法 操作 file.close() 关闭文件 file.fileno() 返回文件的描述符(file descriptor ,FD, 整数值) file.flu ...
- 函数:灵活即强大 - 零基础入门学习Python018
函数:灵活即强大 让编程改变世界 Change the world by program 上节课我们基本介绍了Python函数的用法,非常简单.这节课我们主要针对函数的参数和返回值进一步深入学习.学习 ...
- Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...