[英]Resuming final_suspend causes error, not destroys coroutine
James McNellis in his presentation “Introduction to C++ Coroutines" ( https://youtu.be/ZTqHjjm86Bw?t=1898 ) says the following: James McNellis在他的演讲“ C ++协程简介”( https://youtu.be/ZTqHjjm86Bw?t=1898 )中说:
A coroutine is destroyed when: 在以下情况下,协程会被破坏:
whichever happens first. 以先发生的为准。
In my tests I see (VS 2015, VS 2017 RC), that resuming coroutine that is suspended on final_suspend causes an error instead: 在我的测试中,我看到了(VS 2015,VS 2017 RC),恢复在final_suspend上挂起的协程会导致错误:
Unhandled exception at 0x010B9EDD in Awaits2017.exe: RangeChecks instrumentation code detected an out of range array access. Awaits2017.exe中0x010B9EDD的未处理异常:RangeChecks检测代码检测到超出范围的数组访问。 occurred 发生了
Any ideas what might be going on here? 任何想法可能在这里发生什么?
#include <experimental/resumable>
using namespace std;
using namespace std::experimental;
struct Coro
{
coroutine_handle<> m_coro;
Coro(coroutine_handle<> coro) : m_coro(coro) {}
struct promise_type
{
Coro get_return_object()
{
return Coro(coroutine_handle<promise_type>::from_promise(*this));
}
auto initial_suspend() { return false; }
auto final_suspend() { return true; }
void return_void() {}
};
};
Coro simple()
{
co_return;
}
int main()
{
Coro c = simple();
c.m_coro.resume(); // runtime error here
}
Rule of thumb: 经验法则:
If final_suspend
returns true
, you should call coroutine_handle<>::destroy()
, instead of resume()
. 如果final_suspend
返回true
,则应调用coroutine_handle<>::destroy()
,而不要调用resume()
。
If final_suspend
returns false
, you should not call destroy()
as well, the coroutine will cleanup itself. 如果final_suspend
返回false
,那么您也不应调用destroy()
,协程将自行清理。
Note that the coroutine included in VS 2015 is not what James McNellis shown in the video (the proposal has many revisions), and the description: 请注意,VS 2015中包含的协程并不是视频中显示的James McNellis的内容(该提案进行了多次修订),其描述如下:
final_suspend is resumed final_suspend恢复
may be confusing. 可能会造成混乱。 It does not really mean resume()
is called. 这并不意味着会调用resume()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.