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读写的大文件,数据库操作及网络下载需要很长的时间,为 ...
随机推荐
- Win32多线程编程(3) — 线程同步与通信
一.线程间数据通信 系统从进程的地址空间中分配内存给线程栈使用.新线程与创建它的线程在相同的进程上下文中运行.因此,新线程可以访问进程内核对象的所有句柄.进程中的所有内存以及同一个进程中其他所有线 ...
- IP转发和子网路由
IP地址的分类 在TCP/IP协议中,协议栈分为4层.从上到下依次是应用层.运输层.网络层.网络接口层. IP协议就工作在网络层.IP协议将纷繁复杂的物理层协议屏蔽掉,对上层提供统一的描述和管理服务. ...
- javasscript学习笔记 之 数组学习二 数组的所有方法
1.push() 和 pop() 栈的方法 后进先出 push() 该方法是向数组末尾添加一个或者多个元素,并返回新的长度. push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返 ...
- IPMI 配置BMC用户设置
IPMI 配置BMC用户设置 本文档共介绍5条ipmi设置user的命令,这些命令需要使用root权限才能使用,其中- H为需要操作的BMC ip,-I lanplus为使用rmcp+协议发送命令,- ...
- openssl AES加密算法API的使用示例
openssl为用户提供了丰富的指令,同时也提供了供编程调用的API,本文以使用128位aes算法的ecb模式进行加密和解密验证,如下所示 第一种方法,直接使用aes算法提供的api进行调用,代码如下 ...
- Javascript高级程序设计读书笔记(第三章)
第3章 基本概念 3.4 数据类型 5种简单数据类型:undefined.boolean.number.null.string. typeof操作符,能返回下列某个字符串:“undefined”.“b ...
- 黑马程序员——String类
------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...
- 《第一行代码》学习笔记6-活动Activity(4)
1.SecondActivity不是主活动,故不需要配置标签里的内容. 2.Intent是Android程序中各组件之间进行交互的一种重要方式,一般可被用于 启动活动,启动服务,以及发送广播等.Int ...
- java基础2
//第一个程序 用super访问父类中被隐藏的成员变量和被重写的方法 package foxe; class superClass{ int x; superClass(){ x=4; System. ...
- SQLSERVER内核架构剖析 (转)
我们做管理软件的,主要核心就在数据存储管理上.所以数据库设计是我们的重中之重.为了让我们的管理软件能够稳定.可扩展.性能优秀.可跟踪排错. 可升级部署.可插件运行,我们往往研发自己的管理软件开发平台. ...