Documentation Home >> Headers >> <future> Header >> std::shared_future class template

The std::shared_future class template provides a means of waiting for an asynchronous result from another thread, in conjunction with the std::promise and std::packaged_task class templates which can be used to provide that asynchronous result. Multiple std::shared_future instances can reference the same asynchronous result.

Instances of std::shared_future are CopyConstructible and CopyAssignable. You can also move-construct a std::shared_future from a std::future with the same ResultType.

Accesses to a given instance of std::shared_future are not synchronized. It is therefore not safe for multiple threads to access the same std::shared_future instance without external synchronization. This is in contrast to std::atomic_future. However, accesses to the associated state are synchronized, so it is safe for multiple threads to each access separate instances of std::shared_future that share the same associated state without external synchronization.

template<typename ResultType>
class shared_future
{
public:
    shared_future();
    shared_future(future<ResultType>&&);
    shared_future& operator=(future<ResultType>&&);

    shared_future(shared_future&&);
    shared_future(shared_future const&);
    shared_future& operator=(shared_future const&);
    shared_future& operator=(shared_future&&);
    ~shared_future();

    void swap(shared_future&);

    bool valid() const;
    bool is_ready() const;
    bool has_exception() const;
    bool has_value() const;

    see description get() const;

    void wait() const;

    template<typename Rep,typename Period>
    future_status wait_for(
        std::chrono::duration<Rep,Period> const&
        relative_time) const;

    template<typename Clock,typename Duration>
    future_status wait_until(
        std::chrono::time_point<Clock,Duration> const&
        absolute_time) const;

    template<typename Func>
    future<std::result_of<Func(ResultType)>::type>
    then(Func&& func);

    template<typename Func>
    future<std::result_of<Func(ResultType)>::type>
    then(std::launch policy,Func&& func);
};
Header

#include <future>

See Also