
Environment: Windows XP Boost 1.40 Problem: Canceling an asio::deadline_timer doesn't pass an operation_aborted value to the timer handler. --------------- From the async_wait doc: For each call to async_wait(), the supplied handler will be called exactly once. The handler will be called when: * The timer has expired. * The timer was cancelled, in which case the handler is passed the error code boost::asio::error::operation_aborted. --------------- When I call my Restart() function (which calls the cancel() function) my TimerHandler() receives an err value of 0. When the timer is allowed to expire, the TimerHandler doesn't get called at all. Thoughts? - Bruce Here's the relevant code: boost::asio::io_service mIOService; boost::asio::deadline_timer mTimer(mIOService, boost::posix_time::seconds(3)) inline void TimerHandler(const boost::system::error_code& err) { cout << err; if (err && err != boost::asio::error::operation_aborted) { cout << endl << "Watchdog elapsed" << endl; } } void Start() { mTimer.async_wait(TimerHandler); mIOService.run(); } // Cancels and restarts the watchdog timer - prevents a timeout void Restart() { mTimer.cancel(); mTimer.async_wait(TimerHandler); mIOService.reset(); mIOService.run(); }

Here's the relevant code:
boost::asio::io_service mIOService; boost::asio::deadline_timer mTimer(mIOService, boost::posix_time::seconds(3))
inline void TimerHandler(const boost::system::error_code& err) { cout << err; if (err && err != boost::asio::error::operation_aborted) { cout << endl << "Watchdog elapsed" << endl; } }
void Start() { mTimer.async_wait(TimerHandler); mIOService.run(); }
// Cancels and restarts the watchdog timer - prevents a timeout void Restart() { mTimer.cancel(); mTimer.async_wait(TimerHandler); mIOService.reset(); mIOService.run(); }
The flow is not clear. Your io_service goes out of work and run() exits, then you call Restart()? All this happens on the same thread? If so, try to move reset() to the beginning of the function.

The flow is not clear. Your io_service goes out of work and run() exits, then you call Restart()? All this happens on the same thread? If so, try to move reset() to the beginning of the function. _______________________________________________
Thanks for the reply, Igor You spurred me to compile and test the example. Then I referred to the Boost online book: http://en.highscore.de/cpp/boost/ and the Asynch IO section. Now I have a better idea of how the asio timer works (the run() call blocks and needs to be fired up in a separate thread). - Bruce
participants (2)
-
BKnoth
-
Igor R