[Boost-users] [asio] Timer cancellation behavior

1,105 views
Skip to first unread message

BKnoth

unread,
May 5, 2010, 3:17:06 PM5/5/10
to boost...@lists.boost.org
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();
}

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

--
You received this message because you are subscribed to the Google Groups "BOOST Archives" group.
To post to this group, send email to boost...@googlegroups.com.
To unsubscribe from this group, send email to boost-list+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/boost-list?hl=en.

Igor R

unread,
May 5, 2010, 4:29:18 PM5/5/10
to boost...@lists.boost.org
> 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.

BKnoth

unread,
May 5, 2010, 4:57:19 PM5/5/10
to boost...@lists.boost.org
>
>
> 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
Reply all
Reply to author
Forward
0 new messages