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
#
A future becomes shared by using fut.share()
. A shared future is associated with its promise and can independently ask for the result. An std::shared
future has the same interface as an std::future
.
In addition to the std::future
, a std::shared_future
enables us to query the promise independently of the other associated futures.
There are two ways to create a std::shared_future
:
- Invoke
fut.share()
on anstd::future fut
. Afterwards, the result is no longer available. That meansvalid == false
. - Initialize an
std::shared_future
from anstd::promise
:std::shared_future<int> divResult= divPromise.get_future()
.
The handling of an std::shared_future
is special.
Get hands-on with 1400+ tech skills courses.