匹配的boost :: deadline_timer回调到相应的wait_async [英] Matching boost::deadline_timer callbacks to corresponding wait_async

查看:196
本文介绍了匹配的boost :: deadline_timer回调到相应的wait_async的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这短短的code段,其中一个的boost :: deadline_timer中断另一个问题:

Consider this short code snippet where one boost::deadline_timer interrupts another:

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/asio.hpp>

static boost::asio::io_service io;
boost::asio::deadline_timer timer1(io);
boost::asio::deadline_timer timer2(io);

static void timer1_handler1(const boost::system::error_code& error)
{
    std::cout << __PRETTY_FUNCTION__ << " time:" << time(0) << " error:" << error.message() << " expect:Operation canceled." << std::endl;        
}        

static void timer1_handler2(const boost::system::error_code& error)
{
    std::cout << __PRETTY_FUNCTION__ << " time:" << time(0) << " error:" << error.message() << " expect:success." << std::endl;        
}        

static void timer2_handler1(const boost::system::error_code& error)
{
    std::cout << __PRETTY_FUNCTION__ << " time:" << time(0) << " error:" << error.message() << " expect:success." << std::endl;        
    std::cout << "cancel and restart timer1. Bind to timer1_handler2" << std::endl;
    timer1.cancel();
    timer1.expires_from_now(boost::posix_time::milliseconds(10000));
    timer1.async_wait(boost::bind(timer1_handler2, boost::asio::placeholders::error));        
}        

int main()
{
    std::cout << "Start timer1. Bind to timer1_handler1." << std::endl;
    timer1.expires_from_now(boost::posix_time::milliseconds(2000));
    timer1.async_wait(boost::bind(timer1_handler1, boost::asio::placeholders::error));        

    std::cout << "Start timer2. Bind to timer2_handler1. Will interrupt timer1." << std::endl;
    timer2.expires_from_now(boost::posix_time::milliseconds(2000));
    timer2.async_wait(boost::bind(timer2_handler1, boost::asio::placeholders::error));        

    std::cout << "Run the boost io service." << std::endl;
    io.run();

    return 0;
}

如果定时器2时围绕2秒大关变化,有时timer1_handler1报告成功,有时操作取消。这可能是在简单的例子确定的,因为我们知道什么时候定时器2设置为。

If the time for timer2 is varied around the 2 second mark, sometimes timer1_handler1 reports success, and sometimes operation cancelled. This is probably determinate in the trivial example because we know what time timer2 is set to.

./timer1
Start timer1. Bind to timer1_handler1.
Start timer2. Bind to timer2_handler1. Will interrupt timer1.
Run the boost io service.
void timer1_handler1(const boost::system::error_code&) time:1412680360 error:Success expect:Operation canceled.
void timer2_handler1(const boost::system::error_code&) time:1412680360 error:Success expect:success.
cancel and restart timer1. Bind to timer1_handler2
void timer1_handler2(const boost::system::error_code&) time:1412680370 error:Success expect:success.

这再presents其中Timer1正在实施超时更复杂的系统,而Timer2是一个真正的异步套接字。偶尔我观察其中Timer1被取消为时已晚的情景,而第二async_wait后的第一个处理程序返回()被调用,从而给人一种假超时。

This represents a more complex system where timer1 is implementing a timeout, and timer2 is really an asynchronous socket. Occasionally I've observed a scenario where timer1 is cancelled too late, and the first handler returns after the second async_wait() has been called, thus giving a spurious timeout.

显然,我需要匹配了相应的async_wait()调用的处理程序回调。是否有这样做的一个方便的方法?

Clearly I need to match up the handler callbacks with the corresponding async_wait() call. Is there a convenient way of doing this?

推荐答案

可以的boost ::绑定其他参数来完成处理程序可用于识别源

You can boost::bind additional parameters to the completion handler which can be used to identify the source.

这篇关于匹配的boost :: deadline_timer回调到相应的wait_async的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆