0

Hello I have an interval timer and I would like it to terminate without waiting for it's interval to terminate. How should I do this? I have tried controlling the function with a reading flag. Once the reading flag is set to stop, I would like the function to exit without waiting for one iteration of the interval timer in order to exit.

void send_poll()
{
    // Schedule the timer for the first time:
    timer.async_wait(poll_device);
    // Enter IO loop. The timer will fire for the first time 1 second from now:
    io_service.run();       
}  

void poll_device(const boost::system::error_code& /*e*/)
{
    std:: cout << "message being polled" << std::endl;

    // do polling of device
    if (reading_flag!=1)
    {
        std::terminate();
    }   

    // Reschedule the timer for 1 second in the future:
    timer.expires_at(timer.expires_at() + interval);
    // Posts the timer event
    timer.async_wait(poll_device)
}
Ravi Upadhyay
  • 314
  • 1
  • 3
  • 16
Ariel Baron
  • 331
  • 4
  • 13
  • There is a cancel function for deadline timer object. See http://thisthread.blogspot.in/2011/06/canceling-deadlinetimer.html – Ravi Upadhyay May 01 '17 at 04:09
  • @RaviUpadhyay it's easy to use wrong, see linked duplicate and this one for another description of the background https://stackoverflow.com/questions/31192702/cancel-a-deadline-timer-callback-triggered-anyway/31195803#31195803 – sehe May 01 '17 at 11:31

0 Answers0