c – 我可以使用堆栈协程作为在非常堆栈的协程中定义的steady_ti
我可以用以下方式使用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()指定的块. 当调用 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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |