可以提高.Asio strand.dispatch()块
Can Boost.Asio strand.dispatch() block?
我正在试验Boost。我正在写一个服务器的Asio strand
,我想澄清一些事情。
让我们假设SomeClass
是这样的:
void SomeClass::foo()
{
strand_.dispatch(boost::bind(&SomeClass::bar, this));
}
此外,strand
有一个io_service
,其中有多个线程调用run()
。
在strand::dispatch()
的文档中,我读到它保证通过链发布或调度的处理程序不会并发执行,如果满足这一点,处理程序可以在此函数中执行。是什么决定处理程序是否立即执行?
在多线程的情况下,如果多个io_service线程并发调用SomeClass::foo
, 将调度阻塞?也就是说,除了被执行的语句,其他语句都阻塞了。如果是,处理程序是否按顺序执行(它们称为dispatch()
的顺序)?
从逻辑上讲,如果没有(b)锁定的可能性,就不可能满足这个保证。dispatch
的代码(详见1.46.1中的/impl/strand_service.hpp)证实了这一点。这就是Boost的好处,它都在那里,你可以看到。这里的文档中有关于处理程序调用顺序的信息,包括有时不提供顺序保证的注释。
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);
}
相关文章:
- std::boost::asio::p ost / dispatch 使用哪个io_context?
- 使用boost :: asio :: strand以这种方式安全吗?
- Use boost strand and std::mutex
- asio::strand 上的任务在单个线程上运行
- boost::asio::strand post方法性能
- 提升在多个线程上运行的asio-strand和io_service
- Boost.Python中的dispatch函数和forwarding函数是什么意思
- Boost::Strand 生产者/消费者是否有意义
- boost::asio:"strand"类型的同步原语有什么名称吗?
- asio/strand:为什么计时器的行为不同
- strand在boost asio中的优势是什么?
- 是否strand和io对象(如tcp::socket)需要担心相关联的io_service的生命周期?
- 为什么没有strand::wrap()等价于strand::post()
- 正确使用asio::io_service::strand
- boost::asio::strand wrap导致shared_ptr从shared_from_this变为NULL
- 如何在运行时检查代码是否由给定的asio::strand实例保护
- strand.post 和strand.wrap之间的性能差异是什么?
- Boost::asio::strand::dispatch(句柄)或直接调用句柄
- 可以提高.Asio strand.dispatch()块