7
The Async Function
In the previous chapter, we learned about promises, futures, and packaged tasks. When we introduced packaged tasks, we mentioned that std::async provides a simpler way to achieve the same result, with less code and thus being cleaner and more concise.
The async function (std::async) is a function template that runs a callable object asynchronously where we can also select the method of execution by passing some flags defining the launch policy. It is a powerful tool for handling asynchronous operations, but its automatic management and lack of control over the thread of execution, among other aspects, can also make it unsuitable for certain tasks where fine-grained control or cancellation is required.
In this chapter, we ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access