April 19th, 2021

C++ coroutines: Allowing the awaiter to be destroyed while suspended

One issue that we dealt with when we created our co_await awaitable signal was the case of the awaiter being destroyed while suspended. We had been ignoring that problem in our coroutine promise, but we can’t keep our head in the sand forever. Let’s take a look around and see where we are.

And it turns out that getting rid of the reference count also fixes the problem of an awaiting coroutine being destroyed while suspended: If that happens, the promise_ptr in the awaiter is destructed, and that abandons the coroutine. This means that when the coroutine completes, it will see that nobody is awaiting and won’t try to resume a destroyed awaiting coroutine.

Procrastination pays off!

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.