使用boost线程定时器作为后台线程来切换主循环程序状态方法2
上一篇的方法主要使用的是:通过线程延时实
现的定时,并且只能定时一次,如果需要对此定时处理,就需要使用下面的定时器;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include "stdafx.h" #include <iostream> #include <boost/asio.hpp> #include <boost/thread.hpp> #include <boost/date_time/posix_time/posix_time.hpp> using namespace boost::asio; using namespace boost; using namespace std; //循环状态 enum program_state { state1, state2, state3, state4 }; /* 定时器类的作用:将外部类的超时函数传递进来后,在定时器超时函数中进行调用; 这样类之间的关系,清晰明了 */ class ctimer { private : int _timeout_len; bool _is_remove_when_timeout; // boost::function< void ()> f; //委托函数类型为无参数无返回值 thread * ptrThread; public : //外部函数 // ~ctimer() { if (ptrThread != NULL) { delete this ->ptrThread; this ->ptrThread = NULL; } } template < typename F> void InstallTimer(F f, int _timeout_len, bool _is_remove_when_timeout) { this ->_timeout_len = _timeout_len; this ->_is_remove_when_timeout = _is_remove_when_timeout; this ->f = f; //开启线程 ptrThread= new thread (bind(&ctimer::setTimer, this )); } void RemoveTimer() { this ->_is_remove_when_timeout = true ; this ->_timeout_len = 1; this ->f = NULL; } //定时器线程工作者函数 void setTimer() { io_service m_ios; //设置定时器,并设置回调函数 deadline_timer t( m_ios, boost::posix_time::millisec( this ->_timeout_len)); t.async_wait(bind(&ctimer::timeout, this )); //回调 //阻塞在这里,等待事件 m_ios.run(); cout << "thread quit" <<endl; } //回调函数 void timeout() { //回调函数中调用外部的处理程序 if ( this ->f!=NULL) this ->f(); if ( this ->_is_remove_when_timeout == false ) { thread (bind(&ctimer::setTimer, this )); } } }; //测试类,工作者主类 class TestClassA { public : //定时器类对象 ctimer* ptrTimer; TestClassA() { state = state1; ptrTimer = new ctimer; } ~TestClassA() { delete ptrTimer; } //主程序当前状态 program_state state; //当前状态定时器超时处理函数 void TimeoutCallbackPrint() { printf ( " TimeoutCallbackPrint\n" ); ptrTimer->RemoveTimer(); //直接关闭定时器 this ->state = state2; } void run() { while (1) { //状态循环扫描 switch ( this ->state) { case state1: proc_state1(); break ; case state2: break ; } } } //主程序当前状态,处理函数 void proc_state1() { //加入定时器,并将定时器回调函数的需要处理函数传入进去,并将参数传入 ptrTimer->InstallTimer(bind(&TestClassA::TimeoutCallbackPrint, this ),500, false ); //死循环,进行收发处理 while ( this ->state == state1) { } } }; int _tmain( int argc, _TCHAR* argv[]) { TestClassA *tc = new TestClassA(); tc->run(); //主程序在类中循环 getchar (); return 0; } |
靡不有初,鲜克有终
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!