我可以使用stackful协程为这是非常stackful协同程序中定义的steady_timer的等待处理? [英] Can I use a stackful coroutine as the wait handler of a steady_timer which is defined inside the very stackful coroutine?

查看:403
本文介绍了我可以使用stackful协程为这是非常stackful协同程序中定义的steady_timer的等待处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下方式stackful协同程序和的boost ::支持ASIO :: steady_timer :: async_wait
问题的关键是等待,局部变量定时不是堆栈上,因而无法访问期间(我的理解,不知道)。因此,可以在回调正常进行? (仅供参考,它适用于使用铿锵++ 5.0我的Mac罚款。)

Can I use stackful coroutine and boost::asio::steady_timer::async_wait in the following way? The point is that (my understanding, not sure) during waiting, local variable timer is not on the stack and thus inaccessible. So can the callback proceed normally? (FYI, it works fine on my Mac using clang++5.0 .)

boost::asio::io_service io;
void Work(boost::asio::yield_context yield) {
  boost::asio::steady_timer timer(io);

  timer.expires_from_now(std::chrono::seconds(5));
  timer.async_wait(yield);

  cout << "Woke up." << endl;
}

int main() {
  boost::asio::spawn(io, Work);
  io.run();
  return 0;
}

我觉得这是值得这个问题进行了比较:提高ASIO deadline_timer

推荐答案

是的,它是安全的的boost ::支持ASIO :: yield_context 传递给具有自动对象同协程内的存储时间。

Yes, it is safe to pass boost::asio::yield_context to an object that has automatic storage duration within the same coroutine.

Boost.Coroutine 用途 Boost.Context 执行上下文切换。 Boost.Context提供了暂停当前的执行路径的一种手段,preserving堆栈(包括局部变量,如工作()的 定时器),并将执行控制,使同一个线程来使用不同的协议栈运行。因此,用提高:: ASIO :: steady_timer 具有自动存储持续时间对象,其寿命将结束时,无论

Boost.Coroutine uses Boost.Context to perform context switching. Boost.Context provides a means to suspend the current execution path, preserving the stack (including local variables such as Work()'s timer), and transfer execution control, allowing the same thread to run with a different stack. Hence, with the boost::asio::steady_timer object having automatic storage duration, its lifespan will end when either:


  • 控制退出由指定块的工作()通过是收益,达到函数结束,或异常展开堆栈。

  • 相关联的 io_service对象被破坏。内部处理程序维护共享的协同程序的所有权,而 io_service对象被破坏时,所有相关的处理程序也被破坏。这种破坏会导致Boost.Coroutine迫使每个协同程序的堆栈放松。

  • control exits the block specified by Work() via either a return, reaching end of function, or an exception unwinding the stack.
  • The associated io_service is destroyed. Internal handlers maintain shared ownership of the coroutine, and when the io_service is destroyed, all associated handlers are also destroyed. This destruction will cause Boost.Coroutine to force each coroutine's stack to unwind.

提振:: ASIO ::重生() 被调用,执行Boost.Asio的一些设置工作,然后将<一个href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_service__strand/dispatch.html\"相对=nofollow>讯()一个内部处理程序,将创建一个使用提供的功能为切入点,用户协同程序。当 yield_context 对象作为处理程序异步操作通过后,将Boost.Asio的的收益的启动与完成处理异步操作后立即即会复制结果和的恢复的协程。一个 通过协程拥有的用于保证的收益的发生之前的恢复的。下面是一个试图说明的例子code的执行:

When boost::asio::spawn() is invoked, Boost.Asio performs some setup work and then will dispatch() an internal handler that will create a coroutine using the user provided function as an entry point. When the yield_context object is passed as a handler to asynchronous operations, Boost.Asio will yield immediately after initiating the asynchronous operation with a completion handler that will copy results and resume the coroutine. A strand owned by the coroutine is used to guarantee the yield occurs before resume. Here is an attempt to illustrate the execution of the example code:

boost::asio::io_service io_service;
boost::asio::spawn(io_service, &Work);
`-- dispatch a coroutine creator
    into the io_service.
io_service.run();
|-- invoke the coroutine creator
|   handler.
|   |-- create and jump into
|   |   into coroutine         ----> Work()
:   :                                |-- timer created
:   :                                |-- setting timer expiration
:   :                                |-- 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         :   :
|   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 << "Waked up." << endl;
:   :                                |-- exiting Work() block, timer is 
:   :                                |   destroyed.
|   `-- return                 <---- `-- coroutine done, yielding
`-- no outstanding work in 
    io_service, return.

这篇关于我可以使用stackful协程为这是非常stackful协同程序中定义的steady_timer的等待处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆