简体   繁体   English

更好的提升asio deadline_timer的例子

[英]Better boost asio deadline_timer example

I'm after a better example of the boost::asio::deadline_timer 我正在追求一个更好的例子: boost::asio::deadline_timer

The examples given will always time out and call the close method. 给出的示例将始终超时并调用close方法。 I tried calling cancel() on a timer but that causes the function passed into async_wait to be called immediately. 我尝试在计时器上调用cancel() ,但这会导致传递给async_wait的函数立即被调用。

Whats the correct way working with timers in a async tcp client? 在异步tcp客户端中使用计时器的正确方法是什么?

You mention that calling cancel() on a timer causes the function passed to async_wait to be called immediately. 您提到在计时器上调用cancel()会导致传递给async_wait的函数立即被调用。 This is the expected behavior but remember that you can check the error passed to the timer handler to determine if the timer was cancelled. 这是预期的行为,但请记住,您可以检查传递给计时器处理程序的错误,以确定计时器是否已取消。 If the timer was cancelled, operation_aborted is passed. 如果取消定时器,则传递operation_aborted。 For example: 例如:

void handleTimer(const boost::system::error_code& error) {
    if (error == boost::asio::error::operation_aborted) {
        std::cout << "Timer was canceled" << std::endl;
    }
    else if (error) {
        std::cout << "Timer error: " << error.message() << std::endl;
    }
}

Hopefully this helps. 希望这会有所帮助。 If not, what is the specific example that are you looking for? 如果没有,您正在寻找的具体示例是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM