可以Boost.Asio strand.dispatch()块吗?

时间:2011-04-26 19:54:40

标签: c++ multithreading boost-asio

我正在试验Boost.Asio strand我正在写的服务器,并希望澄清一些事情。

我们假设我们SomeClass有这样的东西:

void SomeClass::foo()
{
    strand_.dispatch(boost::bind(&SomeClass::bar, this));
}

此外,strand有一个io_service,其中有多个线程正在调用run()

strand::dispatch()的文档中,我读到它保证通过strand发布或分派的处理程序不会同时执行,如果满足,则处理程序可能在此函数中执行。是什么决定处理程序是否立即执行?

在多线程的情况下,如果多个io_service线程同时调用SomeClass::foo会调度阻塞吗? I.e。除了被执行的那个之外的所有块。如果是这样,处理程序是按顺序执行的(它们称为dispatch()的顺序)?

1 个答案:

答案 0 :(得分:5)

从逻辑上讲,如果没有(b)锁定的可能性,就不可能满足这种保证。 dispatch的代码(详见/ 1.46.1中的impl / strand_service.hpp)证实了这一点。这对Boost来说是件好事,你可以看到它。在文档here中有关于处理程序调用顺序的信息,包括有时没有提供订购保证的注释。

template <typename Handler>
void strand_service::dispatch(strand_service::implementation_type& impl,
    Handler handler)
{
  // If we are already in the strand then the handler can run immediately.
  if (call_stack<strand_impl>::contains(impl))
  {
    boost::asio::detail::fenced_block b;
    boost_asio_handler_invoke_helpers::invoke(handler, handler);
    return;
  }

  // Allocate and construct an operation to wrap the handler.
  typedef completion_handler<Handler> op;
  typename op::ptr p = { boost::addressof(handler),
    boost_asio_handler_alloc_helpers::allocate(
      sizeof(op), handler), 0 };
  p.p = new (p.v) op(handler);

  // If we are running inside the io_service, and no other handler is queued
  // or running, then the handler can run immediately.
  bool can_dispatch = call_stack<io_service_impl>::contains(&io_service_);
  impl->mutex_.lock();
  bool first = (++impl->count_ == 1);
  if (can_dispatch && first)
  {
    // Immediate invocation is allowed.
    impl->mutex_.unlock();

    // Memory must be releaesed before any upcall is made.
    p.reset();

    // Indicate that this strand is executing on the current thread.
    call_stack<strand_impl>::context ctx(impl);

    // Ensure the next handler, if any, is scheduled on block exit.
    on_dispatch_exit on_exit = { &io_service_, impl };
    (void)on_exit;

    boost::asio::detail::fenced_block b;
    boost_asio_handler_invoke_helpers::invoke(handler, handler);
    return;
  }

  // Immediate invocation is not allowed, so enqueue for later.
  impl->queue_.push(p.p);
  impl->mutex_.unlock();
  p.v = p.p = 0;

  // The first handler to be enqueued is responsible for scheduling the
  // strand.
  if (first)
    io_service_.post_immediate_completion(impl);
}