c++ - Boost.Asio strand.dispatch() 可以阻塞吗?

标签 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 会分派(dispatch) block 吗? 即。除了被执行的那个之外的所有 block 。如果是这样,处理程序是否按顺序执行(它们调用 dispatch() 的顺序)?

最佳答案

从逻辑上讲,如果没有某种 (b) 锁定的可能性,就不可能满足此保证。 dispatch 的代码(来自 1.46.1 的 detail/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);
}

关于c++ - Boost.Asio strand.dispatch() 可以阻塞吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5795704/

相关文章:

boost - 使用 boost::asio 制作异步客户端

c++ - 神秘的200连接从何而来?

c++ - 优化编译器消除错误

c++ - 如何知道输入到 std::array 的最后一个元素是哪个?

c++ - 数据结构的局部性是什么意思?

c++ - 对象从返回值构造而无需复制或 move 构造函数

c++ - 用于 boost::promise<T> 的 Qt 是什么?

java - 从内部类引用的局部变量必须是 final 或有效的 final

wpf - ObservableCollection<T> 不更新 UI

c++ - 写入套接字后管道损坏