1. #include <iostream>
  2. #include <boost/asio.hpp>
  3. int main()
  4. {
  5. boost::asio::io_service io;
  6. boost::asio::deadline_timer timer(io, boost::posix_time::seconds(3));
  7. timer.wait();
  8. std::cout << "Hello, world!\n";
  9. return 0;
  10. }

再来个异步的:

  1. void print(const boost::system::error_code& /*e*/)
  2. {
  3. std::cout << "Hello, world!\n";
  4. }
  5. int main()
  6. {
  7. boost::asio::io_service io;
  8. boost::asio::deadline_timer timer(io, boost::posix_time::seconds(5));
  9. timer.async_wait(&print);
  10. io.run();
  11. return 0;
  12. }

上次演示了基本用法,但它只能发生一次.

问题是怎么定义一个重复发生的定时器(就是隔一定的时间它就会发生一次.)呢,下面的代码就是了, 关键在于回调函数中更改了延时不断的延长定时器

    1. void print(const boost::system::error_code& e,
    2. boost::asio::deadline_timer* t)
    3. {
    4. cout<<"ddd"<<endl;
    5. t->expires_at(t->expires_at()+ boost::posix_time::seconds(1));
    6. t->async_wait(boost::bind(print,boost::asio::placeholders::error,t));
    7. }
    8. void test1()
    9. {
    10. boost::asio::io_service io;
    11. boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
    12. t.async_wait(boost::bind(print,boost::asio::placeholders::error,&t));
    13. io.run();
    14. }
    15. int _tmain(int argc, _TCHAR* argv[])
    16. {
    17. test1();
    18. system("pause");
    19. return 0;
    20. }

boost 定时器.的更多相关文章

  1. 简单的几个Boost定时器

    boost的asio库里有几个定时的器,老的有 deadline_timer , 还有三个可配合 C++11 的 chrono 使用的 high_resolution_timer  . steady_ ...

  2. boost timer 定时器 Operation cancel !

    前面段时间处理一个定时器时,老是提示 操作取消. 硬是没搞明白为什么! 其实我遇到的这个情况很简单就是(boost::asio::deadline_timer timer)这个变量的生命同期结束了,对 ...

  3. 使用boost的deadline_timer实现一个异步定时器

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

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

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

  5. [Boost基础]并发编程——asio网络库——定时器deadline_timer

    asio库基于操作系统提供的异步机制,采用前摄器设计模式(Proactor)实现了可移植的异步(或者同步)IO操作,而且并不要求使用多线程和锁定,有些的避免了多线程编程带来的诸多有害副作用(如条件竞争 ...

  6. boost asio 学习(六) 定时器

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=7 6 定时器 ...

  7. 使用boost线程定时器作为后台线程来切换主循环程序状态方法2

    上一篇的方法主要使用的是:通过线程延时实 现的定时,并且只能定时一次,如果需要对此定时处理,就需要使用下面的定时器: #include "stdafx.h" #include &l ...

  8. Tcp/ip实验准备:一个简单的定时器——boost实现

    tcp/ip实验须要在指定的时间查看结果,为了实验方便,做了一个定时器.用法是: 在命令行输入:timer 输入数字之后,计时对应秒数 输入m数字之后.计时对应分钟数(支持小数分钟数) 输入q退出. ...

  9. boost::asio学习(定时器)

    #include <boost/asio.hpp> #include <iostream> void handle1(const boost::system::error_co ...

随机推荐

  1. Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo

    标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...

  2. Android异步请求

    class MyTask_SendMessage extends AsyncTask<String, Void, String> { @Override protected void on ...

  3. JS中的for和for in循环

    1.for循环通常用来遍历数组或类数组对象 模式1:长度缓存 for(var i=0,max=arr.length;i<max;i++){ //your code } 模式2:逐减,与零比较比与 ...

  4. 4、记录1----获取hdfs上FileSystem的方法 记录2:正则匹配路径:linux、hdfs

    /** * 获取hadoop相关配置信息 * @param hadoopConfPath 目前用户需要提供hadoop的配置文件路径 * @return */ public static Config ...

  5. lightoj 1236 正整数唯一分解定理

    A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     6 ...

  6. C++友元函数重载"++"和"--"运算符

    代码: #include <iostream> #include <cstring> using namespace std; class one{ public: one(i ...

  7. 【JAVA编码】 JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换

    http://blog.csdn.net/qinysong/article/details/1179489 这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记 ...

  8. 一篇详细的 Mysql Explain 详解

    一.语法 explain < table_name > 例如: explain select * from t3 where id=3952602; 二.explain输出解释 +—-+— ...

  9. 序列!序列!- 零基础入门学习Python016

    序列!序列! 让编程改变世界 Change the world by program 你可能发现了,小甲鱼把列表.元组和字符串放在一块儿来讲解是有道理的,我们发现Ta们之间有很多共同点: 1. 都可以 ...

  10. 学习hadoop

    一.笔记本触摸板关闭方法 1.在windows下有官方驱动. 2.ubuntu下没有 操作方法如下: 1,终端操作 临时禁止触摸板:sudo modprobe -r psmouse 开启触摸板:sud ...