Atomically cancel asio asynchronious timer from another thread

限于喜欢 提交于 2019-12-19 10:51:58

问题


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?


回答1:


Actually, the both approaches are not quite safe, just because deadline_timer is not thread-safe.

IMO, the most simple and safe way is to post the cancellation:

//...
timer.get_io_service().post([&]{timer.cancel();})
//...

NOTE: in the real code one has to ensure that timer outlives the functor (lambda).

UPDATE: as @sehe mentioned, this solution might not work - because the cancelling handler may appear in the io_service queue just before print, when the timer is not waiting anymore.



来源:https://stackoverflow.com/questions/29047765/atomically-cancel-asio-asynchronious-timer-from-another-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!