Promises and Futures
In previous chapters, we learned the foundations of managing and synchronizing thread execution using C++. We also mentioned in Chapter 3 that to return values from a thread, we could use promises and futures. Now it’s time to learn how to do that and much more using these features in C++.
Futures and promises are essential blocks for achieving asynchronous programming. They define a way to manage the result of a task that will be completed in the future, usually in a separate thread.
In this chapter, we’re going to cover the following main topics:
- What are promises and futures?
- What are shared futures and how are they different from regular futures?
- What are packaged tasks and when do we use them?
- How do we check future statuses and errors?
- What are the benefits and drawbacks of using promises and futures?
- Examples of real-life scenarios and solutions
So, let’s get started!