Boost学习1--计时器的实现
来源:互联网 发布:2017中国机电贸易数据 编辑:程序博客网 时间:2024/05/16 20:27
Boost是个很强大的C++库,这个系列记录了使用Boost自定义的一系列小工具。
Timer是个很常用的工具,主要功能是每隔一段时间,触发某个事件。在很多其他的开源库中都有类似的小组件,比如QT中的qtimer。TImer中比较核心的地方在于如何触发事件,所谓触发事件,可以认为是唤起某个函数来执行。另外,在Timer运行的过程中,程序的其他线程仍然在工作。所以这会是个多线程运行的工具。
先上代码:
class MyTimer{public:MyTimer(){}~MyTimer(){m_.try_lock();m_.unlock();}void bind_signal(boost::function<void()> f){f_ = f;}void start(double ms){m_.try_lock();m_.unlock();boost::function<void()> func = boost::bind(&MyTimer::sleep_while, this, ms);t_.reset(new boost::thread(func));}void stop(){while (!m_.try_lock());}private:void sleep_while(double t){while (m_.try_lock()){m_.unlock();#if _WIN32//_MSC_VERSleep(t);#elsesleep(t / 1000.0);#endiff_();}}private:boost::function<void()> f_;boost::mutex m_;boost::shared_ptr<boost::thread> t_;};
现在来逐行分析上述代码:
void bind_signal(boost::function<void()> f){f_ = f;}
要触发事件,首先要告诉timer,需要执行什么样的事件(即函数),利用boost::function可以很方便的绑定需要处理的函数,甚至可以动态绑定类的成员函数。
Timer当然需要提供start和stop方法。在此,使用thread和mutex来实现,简单来说,用互斥量来控制sleep_while。
void start(double ms){m_.try_lock();m_.unlock();boost::function<void()> func = boost::bind(&MyTimer::sleep_while, this, ms);t_.reset(new boost::thread(func));}void stop(){while (!m_.try_lock());}
private:void sleep_while(double t){while (m_.try_lock()){m_.unlock();
#if _WIN32//_MSC_VERSleep(t);#elsesleep(t / 1000.0);#endiff_();}}
0 0
- Boost学习1--计时器的实现
- 一个计时器的实现
- AT89C52实现的计时器
- Java 计时器的实现
- Android 计时器的实现
- Javascript 实现的计时器
- 倒计时器的实现
- Chronometer 计时器的实现
- Unity_计时器的实现
- JavaScript实现的计时器和倒计时器
- Boost 计时器 定时器 速记
- boost之progress_timer计时器
- Boost Timer 计时器
- 计时器 boost asio
- javascript实现的倒计时计时器
- 用线程实现的计时器
- C#中计时器的实现
- 安卓中实现计时器的功能
- LintCode-简化路径
- Cocos2dx多线程与异步加载
- SVN命令的使用(Ubuntu)
- The connection to adb is down, and a severe error has occured
- java命名规范
- Boost学习1--计时器的实现
- 6.24
- Labview问题集锦
- 61.找出大于m且紧跟m的素数
- PHP 5.x COM functions提权漏洞的利用分析
- 翻译 - 元编程动态方法之public_send
- Gitolite服务器搭建
- 微博背后的那些算法
- [C/C++] const 详解(修饰变量、输入参数、返回值、成员函数)