<small id='H1WKF'></small><noframes id='H1WKF'>

  • <i id='H1WKF'><tr id='H1WKF'><dt id='H1WKF'><q id='H1WKF'><span id='H1WKF'><b id='H1WKF'><form id='H1WKF'><ins id='H1WKF'></ins><ul id='H1WKF'></ul><sub id='H1WKF'></sub></form><legend id='H1WKF'></legend><bdo id='H1WKF'><pre id='H1WKF'><center id='H1WKF'></center></pre></bdo></b><th id='H1WKF'></th></span></q></dt></tr></i><div id='H1WKF'><tfoot id='H1WKF'></tfoot><dl id='H1WKF'><fieldset id='H1WKF'></fieldset></dl></div>

    <tfoot id='H1WKF'></tfoot>

        • <bdo id='H1WKF'></bdo><ul id='H1WKF'></ul>
      1. <legend id='H1WKF'><style id='H1WKF'><dir id='H1WKF'><q id='H1WKF'></q></dir></style></legend>

        如何在其链的上下文中恢复堆栈式协程的执行?

        How resume the execution of a stackful coroutine in the context of its strand?(如何在其链的上下文中恢复堆栈式协程的执行?)

          <bdo id='fLt2k'></bdo><ul id='fLt2k'></ul>
          <legend id='fLt2k'><style id='fLt2k'><dir id='fLt2k'><q id='fLt2k'></q></dir></style></legend>
            <tbody id='fLt2k'></tbody>

          1. <tfoot id='fLt2k'></tfoot>
            • <i id='fLt2k'><tr id='fLt2k'><dt id='fLt2k'><q id='fLt2k'><span id='fLt2k'><b id='fLt2k'><form id='fLt2k'><ins id='fLt2k'></ins><ul id='fLt2k'></ul><sub id='fLt2k'></sub></form><legend id='fLt2k'></legend><bdo id='fLt2k'><pre id='fLt2k'><center id='fLt2k'></center></pre></bdo></b><th id='fLt2k'></th></span></q></dt></tr></i><div id='fLt2k'><tfoot id='fLt2k'></tfoot><dl id='fLt2k'><fieldset id='fLt2k'></fieldset></dl></div>

              <small id='fLt2k'></small><noframes id='fLt2k'>

                • 本文介绍了如何在其链的上下文中恢复堆栈式协程的执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  using Yield = asio::yield_context;
                  using boost::system::error_code;
                  int Func(Yield yield) {
                    error_code ec;
                    asio::detail::async_result_init<Yield, void(error_code, int)> init(yield[ec]);
                    std::thread th(std::bind(Process, init.handler));
                    int result = init.result.get();  // <--- yield at here
                    return result;
                  }
                  

                  如何实现 Process 以便 Func 将在 Func 最初产生的链的上下文中恢复?

                  How to implement Process so that Func will resumed in the context of the strand that Func was originally spawned on?

                  推荐答案

                  Boost.Asio 使用了一个辅助函数,asio_handler_invoke,为调用策略提供自定义点.例如,当 Handler 被 strand 包裹时,调用策略将导致在调用时通过 strand 调度处理程序.如文档中所述,asio_handler_invoke 应该通过依赖于参数的查找来调用.

                  Boost.Asio uses a helper function, asio_handler_invoke, to provide a customization point for invocation strategies. For example, when a Handler has been wrapped by a strand, the invocation strategy will cause the handler to be dispatched through the strand upon invocation. As noted in the documentation, asio_handler_invoke should be invoked via argument-dependent lookup.

                  using boost::asio::asio_handler_invoke;
                  asio_handler_invoke(nullary_functor, &handler);
                  

                  <小时>

                  对于堆栈式协程,在产生协程和调用 handler_typeyield_context 恢复协程:

                  • 如果代码当前正在协程中运行,则它位于与协程关联的 strand 内.本质上,一个简单的处理程序由恢复协程的 strand 包裹,导致执行跳转到协程,阻塞当前在 strand 中的处理程序.当协程让步时,执行会跳回到 strand 处理程序,让它完成.
                  • 虽然 spawn()io_service(一个将启动并跳转到协程的处理程序)添加工作,协程本身不起作用.为了防止 io_service 事件循环在协程未完成时结束,可能需要在让步之前向 io_service 添加工作.
                  • Stackful 协程使用 strand 来帮助保证协程在 resume 被调用之前yields.Asio 1.10.6/Boost 1.58 启用能够从启动函数中安全地调用完成处理程序.之前的版本要求完成处理程序不能从启动函数中调用,因为它的调用策略将 dispatch(),导致协程在被挂起之前尝试恢复.
                  • If code is currently running in the coroutine, then it is within the strand associated with the coroutine. Essentially, a simple handler is wrapped by the strand that resumes the coroutine, causing execution to jump to the coroutine, blocking the handler currently in the strand. When the coroutine yields, execution jumps back to the strand handler, allowing it to complete.
                  • While spawn() adds work to the io_service (a handler that will start and jump to the coroutine), the coroutine itself is not work. To prevent the io_service event loop from ending while a coroutine is outstanding, it may be necessary to add work to the io_service before yielding.
                  • Stackful coroutines use a strand to help guarantee the coroutine yields before resume is invoked. Asio 1.10.6 / Boost 1.58 enabled being able to safely invoke the completion handler from within the initiating function. Prior versions required that the completion handler was not invoked from within the initiating function, as its invocation strategy would dispatch(), causing the coroutine to attempt resumption before being suspended.

                  这是一个完整的示例,其中说明了这些细节:

                  Here is a complete example that accounts for these details:

                  #include <iostream>    // std::cout, std::endl
                  #include <chrono>      // std::chrono::seconds
                  #include <functional>  // std::bind
                  #include <thread>      // std::thread
                  #include <utility>     // std::forward
                  #include <boost/asio.hpp>
                  #include <boost/asio/spawn.hpp>
                  
                  template <typename CompletionToken, typename Signature>
                  using handler_type_t = typename boost::asio::handler_type<
                    CompletionToken, Signature>::type;
                  
                  template <typename Handler>
                  using async_result = boost::asio::async_result<Handler>;
                  
                  /// @brief Helper type used to initialize the asnyc_result with the handler.
                  template <typename CompletionToken, typename Signature>
                  struct async_completion
                  {
                    typedef handler_type_t<CompletionToken, Signature> handler_type;
                  
                    async_completion(CompletionToken&& token)
                      : handler(std::forward<CompletionToken>(token)),
                        result(handler)
                    {}
                  
                    handler_type handler;
                    async_result<handler_type> result;
                  };
                  
                  template <typename Signature, typename CompletionToken>
                  typename async_result<
                    handler_type_t<CompletionToken, Signature>
                  >::type
                  async_func(CompletionToken&& token, boost::asio::io_service& io_service)
                  {
                    // The coroutine itself is not work, so guarantee the io_service has
                    // work.
                    boost::asio::io_service::work work(io_service);
                  
                    // Initialize the async completion handler and result.
                    async_completion<CompletionToken, Signature> completion(
                        std::forward<CompletionToken>(token));
                  
                    auto handler = completion.handler;
                    std::cout << "Spawning thread" << std::endl;
                    std::thread([](decltype(handler) handler)
                      {
                        // The handler will be dispatched to the coroutine's strand.
                        // As this thread is not running within the strand, the handler
                        // will actually be posted, guaranteeing that yield will occur
                        // before the resume.
                        std::cout << "Resume coroutine" << std::endl;
                        using boost::asio::asio_handler_invoke;
                        asio_handler_invoke(std::bind(handler, 42), &handler);
                      }, handler).detach();
                  
                    // Demonstrate that the handler is serialized through the strand by
                    // allowing the thread to run before suspending this coroutine.
                    std::this_thread::sleep_for(std::chrono::seconds(2));
                  
                    // Yield the coroutine.  When this yields, execution transfers back to
                    // a handler that is currently in the strand.  The handler will complete
                    // allowing other handlers that have been posted to the strand to run.
                    std::cout << "Suspend coroutine" << std::endl;
                    return completion.result.get();
                  }
                  
                  int main()
                  {
                    boost::asio::io_service io_service;
                  
                    boost::asio::spawn(io_service,
                      [&io_service](boost::asio::yield_context yield)
                      {
                        auto result = async_func<void(int)>(yield, io_service);
                        std::cout << "Got: " << result << std::endl;
                      });
                  
                    std::cout << "Running" << std::endl;
                    io_service.run();
                    std::cout << "Finish" << std::endl;
                  }
                  

                  输出:

                  Running
                  Spawning thread
                  Resume coroutine
                  Suspend coroutine
                  Got: 42
                  Finish
                  

                  <小时>

                  有关更多详细信息,请考虑阅读 图书馆基金会异步操作.它提供了关于异步操作的组成、Signature 如何影响 async_result 以及 async_resulthandler_type<的整体设计的更多细节/code> 和 async_completion.


                  For much more details, please consider reading Library Foundations for Asynchronous Operations. It provides much greater detail into the composition of asynchronous operations, how Signature affects async_result, and the overall design of async_result, handler_type, and async_completion.

                  这篇关于如何在其链的上下文中恢复堆栈式协程的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                    <bdo id='VfQuZ'></bdo><ul id='VfQuZ'></ul>

                  • <i id='VfQuZ'><tr id='VfQuZ'><dt id='VfQuZ'><q id='VfQuZ'><span id='VfQuZ'><b id='VfQuZ'><form id='VfQuZ'><ins id='VfQuZ'></ins><ul id='VfQuZ'></ul><sub id='VfQuZ'></sub></form><legend id='VfQuZ'></legend><bdo id='VfQuZ'><pre id='VfQuZ'><center id='VfQuZ'></center></pre></bdo></b><th id='VfQuZ'></th></span></q></dt></tr></i><div id='VfQuZ'><tfoot id='VfQuZ'></tfoot><dl id='VfQuZ'><fieldset id='VfQuZ'></fieldset></dl></div>

                    <small id='VfQuZ'></small><noframes id='VfQuZ'>

                          <legend id='VfQuZ'><style id='VfQuZ'><dir id='VfQuZ'><q id='VfQuZ'></q></dir></style></legend>
                            <tfoot id='VfQuZ'></tfoot>

                              <tbody id='VfQuZ'></tbody>