博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Timer.3 - Binding arguments to a handler
阅读量:4364 次
发布时间:2019-06-07

本文共 3417 字,大约阅读时间需要 11 分钟。

 

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 
#include
#include
#include

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  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 

Return to the 

Previous: 

Next: 

转载于:https://www.cnblogs.com/lvdongjie/p/4461741.html

你可能感兴趣的文章
EASYUI DATAGRID 多列复选框CheckBox
查看>>
fit_transform和transform的区别
查看>>
常用激活函数(激励函数)理解与总结
查看>>
DataFrame.to_dict(orient='dict')英文文档翻译
查看>>
DictVectorizer中的fit_transform
查看>>
HDFS优缺点
查看>>
排序算法(1) 快速排序 C++实现
查看>>
伙伴分配器的一个极简实现
查看>>
$.ajax所犯的错误。success后面不执行
查看>>
Spring注入方式及注解配置
查看>>
cocos2dx blender 骨骼动画实现
查看>>
ARM基础
查看>>
eclipse
查看>>
Mybatis参数传递及返回类型
查看>>
关于Ubuntu使用笔记
查看>>
调整Tomcat上的参数提高性能[转]
查看>>
在Ajax方式产生的浮动框中,点击选项包含某个关键字的选项
查看>>
SDK 操作 list-view control 实例 -- 遍历进程
查看>>
由于SSH配置文件的不匹配,导致的Permission denied (publickey)及其解决方法
查看>>
65. Valid Number
查看>>