Introduction to std::shared_future

This lesson gives an introduction to std::shared_future which is used in C++ for multithreading.

We'll cover the following

std::shared_future

The future creates a shared future by using fut.share(). Shared future is associated with its promise and can independently ask for the result. A std::shared future has the same interface as a std::future.

In addition to the std::future, a std::shared_future enables you to query the promise independently of the other associated futures.

There are two ways to create a std::shared_future:

  1. Invoke fut.share() on a std::future fut. Afterwards, the result in no longer available. That means valid == false
  2. Initialize a std::shared_future from a std::promise: std::shared_future<int> divResult= divPromise.get_future()

The handling of a std::shared_future is special.

Get hands-on with 1200+ tech skills courses.