gpt4 book ai didi

c++ - boost::asio::spawn 是做什么的?

转载 作者:可可西里 更新时间:2023-11-01 18:04:52 27 4
gpt4 key购买 nike

我无法在脑海中形成控制流如何随 spawn 发生的画面。

  1. 当我调用 spawn(io_service, my_coroutine) 时,它是否向 io_service 队列添加了一个新的处理程序来包装对 my_coroutine 的调用?

  2. 当我在协程中调用一个异步函数并将我的 yield_context 传递给它时,它会暂停协程直到异步操作完成吗?

    void my_coroutine(yield_context yield){  ...  async_foo(params ..., yield);  ...   // control comes here only once the async_foo operation completes}

我不明白的是我们如何避免等待。假设 my_coroutine 服务于 TCP 连接,当特定实例暂停时如何调用其他 my_coroutine 实例,等待 async_foo 完成?

最佳答案

简而言之:

  1. 何时 spawn()被调用时,Boost.Asio 执行一些设置工作然后将使用 stranddispatch()一个内部处理程序,它使用用户提供的函数作为入口点来创建协程。在某些情况下,可以在调用 spawn() 时调用内部处理程序,而在其他情况下,它将被发布到 io_service 以进行延迟调用。<
  2. 协程被挂起,直到操作完成并调用完成处理程序,io_service 被销毁,或者 Boost.Asio 检测到协程已被挂起且无法恢复,在Boost.Asio 将在哪一点破坏协程。

如上所述,当 spawn() 被调用时,Boost.Asio 执行一些设置工作,然后将使用 stranddispatch() 一个内部处理程序,它使用用户提供的函数作为入口点来创建协程。当 yield_context 对象作为处理程序传递给异步操作时,Boost.Asio 将在启动异步操作后立即yield,完成处理程序将复制结果和恢复协程。前面提到的 strand 由协程拥有,用于保证 yieldresume 之前发生。让我们考虑一个简单的例子 demonstrating 生成():

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>

boost::asio::io_service io_service;

void other_work()
{
std::cout << "Other work" << std::endl;
}

void my_work(boost::asio::yield_context yield_context)
{
// Add more work to the io_service.
io_service.post(&other_work);

// Wait on a timer within the coroutine.
boost::asio::deadline_timer timer(io_service);
timer.expires_from_now(boost::posix_time::seconds(1));
std::cout << "Start wait" << std::endl;
timer.async_wait(yield_context);
std::cout << "Woke up" << std::endl;
}

int main ()
{
boost::asio::spawn(io_service, &my_work);
io_service.run();
}

上面的例子输出:

Start wait
Other work
Woke up

这里试图说明示例的执行。 |中的路径表示事件栈,:表示挂起栈,箭头表示控制权转移:

boost::asio::io_service io_service;
boost::asio::spawn(io_service, &my_work);
`-- dispatch a coroutine creator
into the io_service.
io_service.run();
|-- invoke the coroutine creator
| handler.
| |-- create and jump into
| | into coroutine ----> my_work()
: : |-- post &other_work onto
: : | the io_service
: : |-- create timer
: : |-- set timer expiration
: : |-- cout << "Start wait" << endl;
: : |-- timer.async_wait(yield)
: : | |-- create error_code on stack
: : | |-- initiate async_wait operation,
: : | | passing in completion handler that
: : | | will resume the coroutine
| `-- return <---- | |-- yield
|-- io_service has work (the : :
| &other_work and async_wait) : :
|-- invoke other_work() : :
| `-- cout << "Other work" : :
| << endl; : :
|-- io_service still has work : :
| (the async_wait operation) : :
| ...async wait completes... : :
|-- invoke completion handler : :
| |-- copies error_code : :
| | provided by service : :
| | into the one on the : :
| | coroutine stack : :
| |-- resume ----> | `-- return error code
: : |-- cout << "Woke up." << endl;
: : |-- exiting my_work block, timer is
: : | destroyed.
| `-- return <---- `-- coroutine done, yielding
`-- no outstanding work in
io_service, return.

关于c++ - boost::asio::spawn 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557582/

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