Timer.3 - Binding arguments to a handler
In this tutorial we will modify the program from tutorial Timer.2 so that the timer fires once a second. This will show how to pass additional parameters to your handler function.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
To implement a repeating timer using asio you need to change the timer's expiry time in your callback function, and to then start a new asynchronous wait. Obviously this means that the callback function will need to be able to access the timer object. To this end we add two new parameters to the print function:
- A pointer to a timer object.
- A counter so that we can stop the program when the timer fires for the sixth time.
void print(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* t, int* count)
{
As mentioned above, this tutorial program uses a counter to stop running when the timer fires for the sixth time. However you will observe that there is no explicit call to ask the io_service to stop. Recall that in tutorial Timer.2 we learnt that the boost::asio::io_service::run() function completes when there is no more "work" to do. By not starting a new asynchronous wait on the timer when count reaches 5, the io_service will run out of work and stop running.
if (*count < 5)
{
std::cout << *count << "\n";
++(*count);
Next we move the expiry time for the timer along by one second from the previous expiry time. By calculating the new expiry time relative to the old, we can ensure that the timer does not drift away from the whole-second mark due to any delays in processing the handler.
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
Then we start a new asynchronous wait on the timer. As you can see, the boost::bind() function is used to associate the extra parameters with your callback handler. The boost::asio::deadline_timer::async_wait() function expects a handler function (or function object) with the signature void(const boost::system::error_code&). Binding the additional parameters converts your print function into a function object that matches the signature correctly.
See the Boost.Bind documentation for more information on how to use boost::bind().
In this example, the boost::asio::placeholders::error argument to boost::bind() is a named placeholder for the error object passed to the handler. When initiating the asynchronous operation, and if using boost::bind(), you must specify only the arguments that match the handler's parameter list. In tutorial Timer.4 you will see that this placeholder may be elided if the parameter is not needed by the callback handler.
t->async_wait(boost::bind(print,
boost::asio::placeholders::error, t, count));
}
} int main()
{
boost::asio::io_service io;
A new count variable is added so that we can stop the program when the timer fires for the sixth time.
int count = 0;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
As in Step 4, when making the call to boost::asio::deadline_timer::async_wait() from main we bind the additional parameters needed for the print function.
t.async_wait(boost::bind(print,
boost::asio::placeholders::error, &t, &count)); io.run();
Finally, just to prove that the count variable was being used in the print handler function, we will print out its new value.
std::cout << "Final count is " << count << "\n"; return 0;
}
See the full source listing
Return to the tutorial index
Previous: Timer.2 - Using a timer asynchronously
Next: Timer.4 - Using a member function as a handler
Timer.3 - Binding arguments to a handler的更多相关文章
- Timer.4 - Using a member function as a handler
In this tutorial we will see how to use a class member function as a callback handler. The program s ...
- boost::asio译文
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...
- Boost.Asio技术文档
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...
- 【转】Timer还是Handler
在我们Android开发过程中,经常需要执行一些短周期的定时任务,这时候有两个选择Timer或者Handler.然而个人认为:Handler在多个方面比Timer更为优秀,更推荐使用. 一.易用性 1 ...
- android Handler vs Timer
Handler vs Timer 在我们Android开发过程中,经常需要执行一些短周期的定时任务,这时候有两个选择Timer或者Handler.然而个人认为: Handler 在多个方面比Timer ...
- 倒计时 总结 Timer Handler CountDownTimer RxJava MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Handler vs Timer,究竟该用哪个?
Handler vs Timer 在我们Android开发过程中,经常需要执行一些短周期的定时任务,这时候有两个选择Timer或者Handler.然而个人认为:Handler在多个方面比Timer更为 ...
- Handler使用总结(转)
方法一:(java习惯,在android平台开发时这样是不行的,因为它违背了单线程模型) 刚刚开始接触android线程编程的时候,习惯好像java一样,试图用下面的代码解决问题 new Thread ...
- Handler的总结
Handler的总结 我们创建的Service.Activity,Broadcast均是一个主线程处理,即UI线程, 但是进行耗时操作时,比如I/O读写的大文件,数据库操作及网络下载需要很长的时间,为 ...
随机推荐
- 有关android 应用的plugin框架调研
1. 借助android提供的shareduserid属性使多个不同的apt共用一个userid,以扫除权限壁垒,获取插件context,继而获取view并加载插件.这种方式是建立在已经安装完成的ap ...
- 解决 innerHTML 在 IE6-IE9中不能赋值的bug
在MSDN可以了解跟多,关于innerHTML的介绍,但是在这里只要是解决表格部分问题 MSDN上有这样的记录: When using innerHTML to insert script, you ...
- css实现超连接按钮形式显示
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- JAVA采用JDBC连接操作数据库详解
JDBC连接数据库概述 一.JDBC基础知识 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供 ...
- hdu find the safest road
算法:多源最短路(floyd) 题意:每条通路有一个安全系数,求始点到终点的最大的安全系数并输出,如果没有输出What a pity! c++超时啊 Problem Description XX星球有 ...
- VMware 虚拟机使用RedHat,出现 connect: Network is unreachable解決方法
http://www.linuxidc.com/Linux/2015-02/113119.htm http://www.osyunwei.com/archives/7829.html
- 空值排序(oracle/sqlserver)
oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 改变空值办法: (1)用nvl函数或decode函数将null转换为一特定值 替 ...
- 库函数 Math
int abs( int num ); double fabs( double arg ); long labs( long num ); 函数返回num的绝对值 #include <mat ...
- [C入门 - 游戏编程系列] 贪吃蛇篇(三) - 蛇定义
蛇是这个游戏的主角,要实现的功能也是最复杂的一个.因为蛇不止有属性,还有行为.它会动,还会吃东西,还会长大!而且还会死!这是很要命的.我一向看不懂复杂的代码,也写不出复杂的代码.所以对于蛇,我很纠结, ...
- Discuz!提取文章标签
<?php //强制使用字符集 @header('Content-Type: text/html; charset=gbk'); $subjectenc ='title'; //这是 ...