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读写的大文件,数据库操作及网络下载需要很长的时间,为 ...
随机推荐
- c#利用VM_COPYDATA实现进程间通信
c#进程间的通信方式很多种,只会这种,感觉比较简单.不懂原理,能用就行. 假设有两个程序:server(主进程),client(子进程) 1.server端: /*定义一个结构体,用来接收从子进程传过 ...
- WIN7中组件服务中的DCOM配置找不到Microsoft Excel应用程序的解决办法
转自:http://blog.csdn.net/lploveme/article/details/8215265 在运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,但是却发现找不到Micro ...
- JS如何得到Repeater中TextBox控件的值
var subsidylCost = document.getElementById("txtSubsidylCost.ClientID").value; 这样获取不到,因为txt ...
- mysql对GIS空间数据的支持,包括创建空间索引
CREATE TABLE tb_geo( id INT PRIMARY KEY AUTO_INCREMENT, NAME ) NOT NULL, pnt POINT NOT NULL, SPATIAL ...
- shell中的case表达式
语法格式 case var in pattern1 | patter2) command1 command2;; pattern3) command1 command2;; *) default co ...
- 【JAVA编码专题】UNICODE,GBK,UTF-8区别
简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...
- angularjs directive 实例 详解
前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...
- 奇怪的JS正则表达式问题
同一个正则表达式,为什么在JS里,用 var reg = new RegExp("..."); 定义,验证就各种失败,用 var reg=/.../; 定义,验证就对了...
- 学习ReactNative笔记整理一___JavaScript基础
学习ReactNative笔记整理一___JavaScript基础 ★★★笔记时间- 2017-1-9 ★★★ 前言: 现在跨平台是一个趋势,这样可以减少开发和维护的成本.第一次看是看的ReactNa ...
- C语言实现的OOP
我倒不是为了OOP而OOP,实在是OOP的一些特征,例如封装,多态其实是软件工程思想,这些思想不分语言,遵循了这些思想可以使得程序更有弹性,更易修改和维护,避免僵化,脆弱 shape.h 该文件定义的 ...