Chinaunix首页 | 论坛 | 博客
  • 博客访问: 894037
  • 博文数量: 299
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2493
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-21 10:07
个人简介

Linux后台服务器编程。

文章分类

全部博文(299)

文章存档

2015年(2)

2014年(297)

分类: C/C++

2014-11-04 10:52:26

deadline_timer和socket一样,都用io_service作为构造函数的参数。也即,在其上进行异步操作,都将导致和io_service所包含的iocp相关联。这同样意味着在析构 io_service之前,必须析构关联在这个io_service上的deadline_timer。

1. 构造函数

在构造deadline_timer时指定时间。
  1. basic_deadline_timer(  
  2.     boost::asio::io_service & io_service);  
  3.   
  4. basic_deadline_timer(  
  5.     boost::asio::io_service & io_service,  
  6.     const time_type & expiry_time);  
  7.   
  8. basic_deadline_timer(  
  9.     boost::asio::io_service & io_service,  
  10.     const duration_type & expiry_time);  
注意后两种的区别。以下2种用法是等价的:
  1. boost::asio::deadline_timer t(io, boost::posix_time::microsec_clock::universal_time()+boost::posix_time::seconds(5));  
  2. boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));  
前者是绝对时间,后者是相对时间。

2. 同步

一个deadline_timer只维护一个超时时间,一个deadline_timer不同时维持多个定时器。
  1. void wait();  
  2. void wait(boost::system::error_code& ec);  
这是个同步等待函数,例如:
  1. boost::asio::io_service io;  
  2. boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));  
  3. t.wait();  
由于不涉及到异步,该函数和io_service没什么关系。这个函数在windows下的实现就只是简单的sleep。因此也就不存在cancel之说。

3. 异步

  1. template<typename WaitHandler>  
  2. void async_wait(WaitHandler handler);  
注意这个error很重要,表明这个handler是因为超时被执行还是因为被cancel。
符合2种情况之一,handler被执行:超时或者被cancel。
这同时隐含的说明了除非io.stop被调用,否则handler一定会被执行。即便是被cancel。
被cancel有多种方法,直接调用cancel或者调用expires_at,expires_from_now重新设置超时时间。

4. 例子

  1. namespace  
  2. {  
  3.     void print(const boost::system::error_code&)  
  4.     {  
  5.         PRINT_DEBUG("Hello, world!");  
  6.     }  
  7.   
  8.     void handle_wait(const boost::system::error_code& error,  
  9.                      boost::asio::deadline_timer& t,   
  10.                      int& count)  
  11.     {  
  12.         if(!error)  
  13.         {  
  14.             PRINT_DEBUG(count);  
  15.             if(count++ < 5)  
  16.             {  
  17.                 t.expires_from_now(boost::posix_time::seconds(3));  
  18.                 t.async_wait(boost::bind(handle_wait,   
  19.                                          boost::asio::placeholders::error,  
  20.                                          boost::ref(t),  
  21.                                          boost::ref(count)));  
  22.                 if (count == 3)  
  23.                 {  
  24.                     t.cancel();  
  25.                 }  
  26.   
  27.             }  
  28.         }  
  29.     }   
  30. }  
  31.   
  32. // 同步方法  
  33. void test_timer_syn()  
  34. {  
  35.     boost::asio::io_service ios;  
  36.     boost::asio::deadline_timer t(ios, boost::posix_time::seconds(3));  
  37.     PRINT_DEBUG(t.expires_at());  
  38.     t.wait();  
  39.     PRINT_DEBUG("Hello syn deadline_timer!");  
  40. }  
  41.   
  42. // 异步方法: 3秒后执行print方法.   
  43. void test_timer_asyn()  
  44. {  
  45.     boost::asio::io_service io;  
  46.   
  47.     boost::asio::deadline_timer t(io, boost::posix_time::seconds(3));  
  48.     t.async_wait(print);  
  49.     PRINT_DEBUG("After async_wait...");  
  50.     io.run();  
  51. }  
  52.   
  53. // 异步循环执行方法:   
  54. void test_timer_asyn_loop()  
  55. {  
  56.     boost::asio::io_service io;  
  57.     boost::asio::deadline_timer t(io);  
  58.     size_t a = t.expires_from_now(boost::posix_time::seconds(1));  
  59.   
  60.     int count = 0;  
  61.     t.async_wait(boost::bind(handle_wait,   
  62.                              boost::asio::placeholders::error,  
  63.                              boost::ref(t),  
  64.                              boost::ref(count)));  
  65.     io.run();      
  66. }  
阅读(6821) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~