从另一个线程以原子方式取消asio异步计时器 [英] Atomically cancel asio asynchronious timer from another thread

查看:124
本文介绍了从另一个线程以原子方式取消asio异步计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定期运行的加速截止时间(例如 http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tuttimer3/src.html ):

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;
}

现在我需要从另一个线程中取消它. 但是,如果取消调用仅在打印功能执行期间出现,而在expires_at调用之前出现,该怎么办?然后计时器将继续运行.

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?

推荐答案

实际上,这两种方法都不是很安全,只是因为deadline_timer

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

IMO,最简单和安全的方法是post取消:

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

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

注意:在真实代码中,必须确保timer超过函子(lambda).

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

更新:如@sehe所述,此解决方案可能不起作用-因为当计时器不再等待时,取消处理程序可能出现在print之前的io_service队列中.

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.

这篇关于从另一个线程以原子方式取消asio异步计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆