gpt4 book ai didi

c++ - boost::asio strand 包裹的线程不能立即工作

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:43 25 4
gpt4 key购买 nike

我最近使用了 boost::asio::strand。我对此没有经验。

如果我用 strand::wrap() 调用 function1 并用 调用 function2 ,我遇到了什么~function1 中的 strand::wrap()function2 正在等待 function1 完成。

ioservice->post(m_strand.wrap(boost::bind( function1 )));
ioservice->post(m_strand.wrap(boost::bind( function2 )));

如果我像这样更改代码,它会按我的预期工作

ioservice->post(m_strand.wrap(boost::bind( function1 )));
ioservice->post((boost::bind( function2 )));

这是因为 m_strand 正在排队函数调用吗?

我在搜索中发现了什么;

A boost::asio::strand guarantees that, for those handlers that are dispatched through it, an executing handler will be allowed to complete before the next one is started. This is guaranteed irrespective of the number of threads that are calling io_service::run(). Of course, the handlers may still execute concurrently with other handlers that were not dispatched through an boost::asio::strand, or were dispatched through a different boost::asio::strandobject.

When initiating the asynchronous operations, each callback handler is wrapped using the boost::asio::strand object. The strand::wrap() function returns a new handler that automatically dispatches its contained handler through the boost::asio::strand object. By wrapping the handlers using the same boost::asio::strand, we are ensuring that they cannot execute concurrently.

在不等待 function1 完成的情况下执行 function2 我必须做什么? (我知道我可以在没有线程或链的情况下执行它,但我想用 strand::wrap() 调用它)

最佳答案

链被明确设计为一次只允许执行一个函数。

如果您使用异步方法,那么当一个函数正在等待异步方法返回时,其他函数将被允许在同一链上执行。

如果您不使用异步方法,那么 strands 将按照您观察到的那样简单地串行执行发布的函数。

关于c++ - boost::asio strand 包裹的线程不能立即工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54321281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com