概述

最近在工作上需要用到定时器,然后看到boost里面的deadline_timer可以实现一个定时器,所以就直接将其封装成了ATimer类,方便使用,ATimer有以下优点:

  1. 可以支持纳秒、毫秒、秒、分、小时定时。
  2. 可以随时停止定时器。
  3. 支持单次调用。
  4. 因为使用了deadline_timer,所以定时比较准确。

ATimer和Qt的QTimer使用方法类似,若没有类似的Timer类,使用最原始的方法,我们的代码可能会是这样的:

m_timerThread = std::thread([this]
{
while (!m_bThreadStoped)
{
++m_sleepCount;
Sleep(SLEEP_DURATION_TIME); if (m_sleepCount == m_sleepAllCount)
{
m_sleepCount = 0;
doSomeThing();
}
}
});

若使用QTimer的话,书写是这样的:

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);

再来看看ATimer的使用:

ATimer<> t;
t.bind([]{ std::cout << "Hello C++" << std::endl; });
t.start(1000);

从上面的例子可以看到,QTimer和ATimer的使用都非常方便,接下来看看ATimer的具体实现:

// ATimer.hpp
#ifndef _ATIMER_H
#define _ATIMER_H #include <vector>
#include <thread>
#include <atomic>
#include <functional>
#include <boost/timer.hpp>
#include <boost/asio.hpp> template<typename Duration = boost::posix_time::milliseconds>
class ATimer
{
public:
ATimer() : m_timer(m_ios, Duration(0)), m_isSingleShot(false) {}
~ATimer()
{
stop();
} void start(unsigned int duration)
{
if (m_ios.stopped())
{
return;
} m_isActive = true;
m_duration = duration;
m_timer.expires_at(m_timer.expires_at() + Duration(m_duration));
m_func = [this]
{
m_timer.async_wait([this](const boost::system::error_code&)
{
for (auto& func : m_funcVec)
{
func();
} if (!m_isSingleShot)
{
m_timer.expires_at(m_timer.expires_at() + Duration(m_duration));
m_func();
}
});
}; m_func();
m_thread = std::thread([this]{ m_ios.run(); });
} void stop()
{
m_ios.stop();
if (m_thread.joinable())
{
m_thread.join();
}
m_isActive = false;
} void bind(const std::function<void()>& func)
{
m_funcVec.emplace_back(func);
} void setSingleShot(bool isSingleShot)
{
m_isSingleShot = isSingleShot;
} bool isSingleShot() const
{
return m_isSingleShot;
} bool isActive() const
{
return m_isActive;
} private:
boost::asio::io_service m_ios;
boost::asio::deadline_timer m_timer;
std::function<void()> m_func = nullptr;
std::vector<std::function<void()>> m_funcVec;
std::thread m_thread;
unsigned int m_duration = 0;
std::atomic<bool> m_isSingleShot;
bool m_isActive = false;
}; #endif

下面是ATimer的具体使用例子:

// main.cpp
#include <iostream>
#include "ATimer.hpp" void test()
{
std::cout << "Timer thread id: " << std::this_thread::get_id() << std::endl;
} int main()
{
std::cout << "Main thread id: " << std::this_thread::get_id() << std::endl; ATimer<boost::posix_time::minutes> t0;
t0.setSingleShot(true);// 单次调用
t0.bind(test);
t0.start(1);// 一分钟之后调用 ATimer<> t;//默认使用毫秒定时器
t.bind(test);
t.bind([]{ std::cout << "Hello C++" << std::endl; });
t.start(1000);//每1000ms调用一次 std::cin.get();
t0.stop();
t.stop();
std::cout << "Tiemr stop" << std::endl; std::cin.get();
std::cout << "Process end" << std::endl; return 0;
}

该例子的github地址:https://github.com/chxuan/samples/tree/master/ATimer

使用boost的deadline_timer实现一个异步定时器的更多相关文章

  1. boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等

    一.使用同步定时器 这个示例程序通过展示如何在一个定时器执行一个阻塞等待. //makefile #-------------------------------------------------- ...

  2. boost::asio::deadline_timer(理解)

    并发与并行: 并发和并行从宏观上来讲都是同时处理多路请求的概念.但并发和并行又有区别,并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔内发生. 1.Timer.1 - 使 ...

  3. c++11实现异步定时器

    c++11提供了丰富的时间和线程操作函数,比如 std::this_thread::sleep, std::chrono::seconds等.可以利用这些来很方便的实现一个定时器.     定时器要求 ...

  4. 深入理解Tornado——一个异步web服务器

    本人的第一次翻译,转载请注明出处:http://www.cnblogs.com/yiwenshengmei/archive/2011/06/08/understanding_tornado.html原 ...

  5. 分享一个异步任务在遇到IO异常时支持递归回调的辅助方法

    public void TryAsyncActionRecursively<TAsyncResult>( string asyncActionName, Func<Task<T ...

  6. 一个异步任务接收两个url下载两个图片

    有两个url,一个是下载用户头像的url,一个是下载用户上传图片的url,想要用一个异步任务同时下载这两个图片. 程序的下载任务是这么执行的,先接受url参数,然后调用 imgUrls = infoP ...

  7. Netty实现的一个异步Socket代码

    本人写的一个使用Netty实现的一个异步Socket代码 package test.core.nio; import com.google.common.util.concurrent.ThreadF ...

  8. 并发编程 —— 自己写一个异步回调 API

    1. 前言 在并发编程中,异步回调的效率不言而喻,在业务开发中,如果由阻塞的任务需要执行,必然要使用异步线程.并且,如果我们想在异步执行之后,根据他的结果执行一些动作. JDK 8 之前的 Futur ...

  9. 如何设计一个异步Web服务——任务调度

    接上一篇<如何设计一个异步Web服务——接口部分> Application已经将任务信息发到了Service服务器中,接下来,Service服务器改如何对自身的资源进行合理分配以满足App ...

随机推荐

  1. Command Line Tools uninstall

    sudo rm -rf /Library/Developer/CommandLineTools

  2. <一道题>abc+cba=1333,求满足条件的abc的值,隐含条件a!=0,c!=0

    这类东西,无非就是穷举法.见下面代码: #include <stdio.h> #include <stdlib.h> /* *abc + cba = 1333 * *a = ? ...

  3. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  4. HDU1151Air Raid(二分图的最大匹配)

    题目大意: 有一个城镇,它的所有街道都是单行的,并且每条街道都是和两个路口相连.同时已知街道不会形成回路. 你的任务是编写程序求最小数量的伞兵,这些伞兵可以访问(visit)所有的路口.

  5. 【tcl脚本】改变输出字符格式

    需求: 原list输出格式 0x00 0x50 0x01 0x03 0x04 0x02 0x21 0x57 0x01 0x00 0x05 0x0B 0x03 0x13 0x00 0x01 要求list ...

  6. Spring中使用Hibernate

    在context中定义DataSource,创建SessionFactoy,设置参数: DAO类继承HibernateDaoSupport,实现具体接口,从中获得HibernateTemplate进行 ...

  7. AS:加载新版本的SWF文件。

    方案一: 文件名+版本号,区别对待不同的版本控制,有设定值后会加上_v_x的后缀名.如:加载主文件 main.swf, 被命名为:Main_v_60.swf . 方案二: loader.load(ne ...

  8. 【css hack】正是我所找的,帮了大忙啊

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-03-05) 各个浏览器单独设置属性 IE6:能识别下划线 “_” 和 星号 “*“,不能识别 “!important”    ...

  9. How a non-windowed component can receive messages from Windows -- AllocateHWnd

    http://www.delphidabbler.com/articles?article=1 Why do it? Sometimes we need a non-windowed componen ...

  10. IAR EWARM Example Download List

    https://srv.iar.com/ExamplesOnDemand/versions.xml http://netstorage.iar.com/SuppDB/Public/EXAMPLES/0 ...