I have a boost deadline_timer which runs periodically (as in example http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tuttimer3/src.html):
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* t)
{
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
t->async_wait(boost::bind(print,
boost::asio::placeholders::error, t, count));
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
t.async_wait(boost::bind(print,
boost::asio::placeholders::error, &t));
io.run();
return 0;
}
Now I need to cancel it from another thread. But what if the call of cancel appears just during print function execution but before expires_at call? Then timer will continue to run.
One way to deal with it is to run something like
while (timer.cancel() == 0) {
}
in that separate thread function.
But maybe somebody knows more elegant way this issue can be done?