C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 我可以使用堆栈协程作为在非常堆栈的协程中定义的steady_timer的等待处理程序吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以用以下方式使用stackful coroutIne和boost :: asio :: steady_timer :: async_wait吗?
重点是(我的理解,不确定)在等待期间,局部变量计时器不在堆栈上,因此无法访问.那么回调能否正常进行? (仅供参,使用clang 5.0在我的Mac上运行正常.)
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;
}

我认为值得对这个问题进行比较:boost asio deadline_timer

解决方法

是的,将boost :: asio :: yield_context传递给在同一个协程中具有自动存储持续时间的对象是安全的.

Boost.Coroutine使用Boost.Context执行上下文切换. Boost.Context提供了一种挂起当前执行路径,保留堆栈(包括Work()的定时器等局部变量)和传输执行控制的方法,允许同一个线程与不同的堆栈一起运行.因此,使用具有自动存储持续时间的boost :: asio :: steady_timer对象,其寿命将在以下任一情况下结束:

> control通过返回,到达函数结尾或展开堆栈的异常退出Work()指定的块.
>相关的io_service被销毁.内部处理程序维护协同程序的共享所有权,当io_service被销毁时,所有关联的处理程序也会被销毁.这种破坏将导致Boost.CoroutIne强制每个协程的堆栈展开.

调用boost::asio::spawn()时,Boost.Asio执行一些设置工作,然后dispatch()将使用用户提供的函数作为入口点创建协程的内部处理程序.当yield_context对象作为处理程序传递给异步操作时,Boost.Asio将在使用完成处理程序启动异步操作后立即生成,该处理程序将复制结果并恢复协程.由协程拥有的strand用于保证在恢复之前产生收益.这是尝试说明示例代码的执行:

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.

大佬总结

以上是大佬教程为你收集整理的c – 我可以使用堆栈协程作为在非常堆栈的协程中定义的steady_timer的等待处理程序吗?全部内容,希望文章能够帮你解决c – 我可以使用堆栈协程作为在非常堆栈的协程中定义的steady_timer的等待处理程序吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。