November 1st, 2021

Why do all this coroutine stuff if you can just use std::future?

One of my colleagues reacted to my series on creating custom coroutines by asking, “Why did you bother with this simple_task custom coroutine? Why not just use std::future objects produced by std::async? You can co_await a std::future.”

Okay, first of all, the ability to co_await a std::future is not a feature of the standard (as of this writing). It’s something that the Microsoft STL provides as a courtesy. And that courtesy has not always been smooth sailing.

For another thing, std::async lets you pick how the lambda runs, but your only options are to run it on a separate thread, or to run the lambda only when the value is awaited. The lambda itself can’t do any co_awaiting because it is not a coroutine. It’s just a plain synchronous lambda.

But the real kicker is that the Microsoft STL implementation of std::future is written in… PPL, The very library we are trying to get rid of by switching to coroutines in the first place.

That last bit was the clincher as far as my colleague was concerned. He sent me the astronaut with a gun meme, but instead of asking, “Wait, it’s all Ohio?”, the first astronaut asks, “std::future is just PPL?” The reply from the astronaut with the gun remains “Always has been.”

Meme background: Wait it’s all Ohio? Always has been.

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.

1 comment

Discussion is closed. Login to edit/delete existing comments.

Newest
Newest
Popular
Oldest

Feedback