c – boost :: asio :: spawn有什么作用?

前端之家收集整理的这篇文章主要介绍了c – boost :: asio :: spawn有什么作用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法形成关于控制流如何与spawn发生的精神图景.

>当我调用spawn(io_service,my_coroutine)时,它是否会向io_service队列添加一个新的处理程序来包装对my_coroutine的调用
>当在协同程序中我调用异步函数传递我的yield_context时,是否挂起协同程序直到异步操作完成?

  1. void my_coroutine(yield_context yield)
  2. {
  3. ...
  4. async_foo(params ...,yield);
  5. ... // control comes here only once the async_foo operation completes
  6. }

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

解决方法

简而言之:

>当调用spawn()时,Boost.Asio执行一些设置工作,然后将使用stranddispatch()内部处理程序,该处理程序使用用户提供的函数作为入口点创建协同程序.在某些条件下,可以在对spawn()的调用调用内部处理程序,有时将其发布到io_service以进行延迟调用.
>协程暂停,直到操作完成并调用完成处理程序,io_service被销毁,或者Boost.Asio检测到协程已被挂起而无法恢复它,此时Boost.Asio将破坏协同程序.

如上所述,当调用spawn()时,然后使用strand来调度()内部处理程序,该处理程序使用用户提供的函数作为入口点创建协同程序.当yield_context对象作为处理程序传递给异步操作时,Boost.Asio将在使用完成处理程序启动异步操作后立即生成,该处理程序将复制结果并恢复协程.前面提到的链是由coroutine所有,用于保证在恢复之前产生的产量.让我们考虑一个简单的例子demonstrating spawn():

  1. #include <iostream>
  2. #include <boost/asio.hpp>
  3. #include <boost/asio/spawn.hpp>
  4.  
  5. boost::asio::io_service io_service;
  6.  
  7. void other_work()
  8. {
  9. std::cout << "Other work" << std::endl;
  10. }
  11.  
  12. void my_work(boost::asio::yield_context yield_context)
  13. {
  14. // Add more work to the io_service.
  15. io_service.post(&other_work);
  16.  
  17. // Wait on a timer within the coroutine.
  18. boost::asio::deadline_timer timer(io_service);
  19. timer.expires_from_now(boost::posix_time::seconds(1));
  20. std::cout << "Start wait" << std::endl;
  21. timer.async_wait(yield_context);
  22. std::cout << "Woke up" << std::endl;
  23. }
  24.  
  25. int main ()
  26. {
  27. boost::asio::spawn(io_service,&my_work);
  28. io_service.run();
  29. }

以上示例输出

  1. Start wait
  2. Other work
  3. Woke up

这是尝试说明示例的执行.路径|表示活动堆栈,:表示挂起的堆栈,箭头用于表示控制转移:

  1. boost::asio::io_service io_service;
  2. boost::asio::spawn(io_service,&my_work);
  3. `-- dispatch a coroutine creator
  4. into the io_service.
  5. io_service.run();
  6. |-- invoke the coroutine creator
  7. | handler.
  8. | |-- create and jump into
  9. | | into coroutine ----> my_work()
  10. : : |-- post &other_work onto
  11. : : | the io_service
  12. : : |-- create timer
  13. : : |-- set timer expiration
  14. : : |-- cout << "Start wait" << endl;
  15. : : |-- timer.async_wait(yield)
  16. : : | |-- create error_code on stack
  17. : : | |-- initiate async_wait operation,: : | | passing in completion handler that
  18. : : | | will resume the coroutine
  19. | `-- return <---- | |-- yield
  20. |-- io_service has work (the : :
  21. | &other_work and async_wait) : :
  22. |-- invoke other_work() : :
  23. | `-- cout << "Other work" : :
  24. | << endl; : :
  25. |-- io_service still has work : :
  26. | (the async_wait operation) : :
  27. | ...async wait completes... : :
  28. |-- invoke completion handler : :
  29. | |-- copies error_code : :
  30. | | provided by service : :
  31. | | into the one on the : :
  32. | | coroutine stack : :
  33. | |-- resume ----> | `-- return error code
  34. : : |-- cout << "Woke up." << endl;
  35. : : |-- exiting my_work block,timer is
  36. : : | destroyed.
  37. | `-- return <---- `-- coroutine done,yielding
  38. `-- no outstanding work in
  39. io_service,return.

猜你在找的C&C++相关文章