Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better boost asio deadline_timer example

I'm after a better example of the boost::asio::deadline_timer

The examples given will always time out and call the close method. I tried calling cancel() on a timer but that causes the function passed into async_wait to be called immediately.

Whats the correct way working with timers in a async tcp client?

like image 902
hookenz Avatar asked Dec 17 '09 01:12

hookenz


1 Answers

You mention that calling cancel() on a timer causes the function passed to async_wait to be called immediately. 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. 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?

like image 106
Yukiko Avatar answered Sep 23 '22 22:09

Yukiko