一.构造函数

  一个deadline_timer只维护一个超时时间,一个deadline_timer不同时维护多个定时器。在构造deadline_timer时指定时间:

 basic_deadline_timer(boost::asio::io_service & io_service);

 basic_deadline_timer( boost::asio::io_service & io_service,
const time_type & expiry_time); basic_deadline_timer(boost::asio::io_service & io_service,
const duration_type & expiry_time);

二.同步定时器

  由于不涉及到异步,该函数和io_service没有什么关系。只是简单的sleep。

 boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds());
t.wait();

三.异步定时器

  由于涉及到异步,该函数需要io_service来运行run,进行调度。

 boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds());
t.async_wait(handler);

四.例子

 #include<boost/asio.hpp>
#include<boost/ref.hpp>
#include<iostream> using namespace std;
typedef function<void (const boost::system::error_code&)> timer_callback ; void print(const boost::system::error_code&)
{
cout << "async timer."<<endl;
} void bindPrint(const boost::system::error_code & err,boost::asio::deadline_timer &timer)
{
cout<<"bind loop async timer."<<endl;
timer.expires_at(timer.expires_at() + boost::posix_time::seconds());
timer.async_wait(std::bind(bindPrint, std::placeholders::_1, boost::ref(timer)));
} int main()
{
boost::asio::io_service io; //1.基本的同步定时器
boost::asio::deadline_timer timer1(io, boost::posix_time::seconds());
timer1.wait(); //2.基本的异步定时器
boost::asio::deadline_timer timer2(io, boost::posix_time::seconds());
timer2.async_wait(print); //3.使用lambda来生成回调函数
boost::asio::deadline_timer timer3(io, boost::posix_time::seconds());
timer_callback callback = [&](const boost::system::error_code& err)
{
cout<<"lambda loop async timer."<<endl;
timer3.expires_at(timer3.expires_at() + boost::posix_time::seconds());
timer3.async_wait(callback);
};
timer3.async_wait(callback); //4.使用bind来生成回调函数
boost::asio::deadline_timer timer4(io, boost::posix_time::seconds());
timer4.async_wait(std::bind(bindPrint, std::placeholders::_1, boost::ref(timer4))); io.run();
return ;
}

五.补充

1.deadline_timer的计时是在定义后,即timer构造函数完成后就开始,而不是调用wait()或者async_wati()后开始计时。

2.deadline_timer不能进行拷贝的,所以在bind中必须使用boost::ref进行包裹。

boost.asio系列(一)——deadline_timer的更多相关文章

  1. boost.asio系列——Timer

    同步Timer asio中提供的timer名为deadline_timer,它提供了超时计时的功能.首先以一个最简单的同步Timer为例来演示如何使用它. #include<iostream&g ...

  2. boost.asio系列——socket编程

    asio的主要用途还是用于socket编程,本文就以一个tcp的daytimer服务为例简单的演示一下如何实现同步和异步的tcp socket编程. 客户端 客户端的代码如下: #include &l ...

  3. boost.asio系列——io_service

    IO模型 io_service对象是asio框架中的调度器,所有异步io事件都是通过它来分发处理的(io对象的构造函数中都需要传入一个io_service对象). asio::io_service i ...

  4. boost.asio系列——buffer

    创建buffer 在io操作中,对数据的读写大都是在一个缓冲区上进行的,在asio框架中,可以通过asio::buffer函数创建一个缓冲区来提供数据的读写.buffer函数本身并不申请内存,只是提供 ...

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

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

  6. boost.asio源码剖析(五) ---- 泛型与面向对象的完美结合

    有人说C++是带类的C:有人说C++是面向对象编程语言:有人说C++是面向过程与面向对象结合的语言.类似的评论网上有很多,虽然正确,却片面,是断章取义之言. C++是实践的产物,C++并没有为了成为某 ...

  7. 如何在多线程leader-follower模式下正确的使用boost::asio。

    #include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...

  8. BOOST.Asio——Tutorial

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  9. BOOST.Asio——Overview

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

随机推荐

  1. 六、python沉淀之路--int str list tuple dict 重点总结

    一.数字int(..)二.字符串replace/find/join/strip/startswith/split/upper/lower/formattempalte = "i am {na ...

  2. 宝塔中的 base_opendir chattr

    宝塔中的 base_opendir chattr base_opendir 目的是限制一些函数将手乱伸. 而宝塔中的自动配置的是 .user.ini,这个是文件是 +i ............ 这个 ...

  3. PHP数组编码转换

    因为一些特殊字符的显示效果的原因不得不把习惯的utf-8工程改成了GBK,由于使用了ajax技术,又涉及到了老问题——编码转换. 一些表单验证需要返回json数据,php的json_encode函数只 ...

  4. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  5. 基于dubbo的SOA项目改造

    工程改造: 1.原来工程删除掉表现层的模块,将表现层独立出来. 2.将原来的工程改造.将service的打包方式改为改为war. 3.在service模块中添加web.xml文件 4.在web.xml ...

  6. MySQL 利用xtrabackup进行增量备份详细过程汇总 (转)

    Xtrabackup下载.安装以及全量备份请参考:http://blog.itpub.net/26230597/viewspace-1465772/ 1,创建mysql备份用户 mysql -uroo ...

  7. Azure CLI的版本问题

    Azure支持多种管理方法.命令行方法有: PowerShell,PowerShell只能运行在Windows上 Azure CLI,而Azure CLI可以运行在Windows.MAC以及Linux ...

  8. 我和domino不得不说的故事(连载2016-3-2)

    1.关于NotesViewEntry 注意:通过NotesViewEntry获取某列的值时,若该列的值为@IsExpandable or @DocNumber 或者是常量时,将不会显示. Set en ...

  9. web基础 (二) html标签

    一.html是什么? 超文本标记语言(Hypertext Markup Language,HTML)通过标签语言来标记要显示的网页中的各个部分.一套规则,浏览器认识的规则 浏览器按顺序渲染网页文件,然 ...

  10. The lesser known pitfalls of allowing file uploads on your website

    These days a lot of websites allow users to upload files, but many don’t know about the unknown pitf ...