Skip to content

Latest commit

 

History

History
237 lines (187 loc) · 8.32 KB

6.1 Future-header-synopsis.md

File metadata and controls

237 lines (187 loc) · 8.32 KB

本小节主要介绍 C++11标准 <future> 头文件中类和函数的摘要。

C++11 标准中与异步任务系相关的类型主要是以下四种 std::promisestd::packaged_taskstd::promisestd::packaged_task 也称为异步任务的提供者 Provider,此外 std::async 也可以作为异步任务的提供者,不过 std::async 并不是类,而是函数,本章后面会详细介绍异步任务的提供者 Provider),std::futurestd::shared_future。另外 <future> 中还定义一些辅助的类,例如: std::future_errorstd::future_errcstd::statusstd::launch

下面我们看看 C++11 标准中是怎么声明以上类型和函数的。

6.1 <future> 头文件摘要

6.1.1 <future> 头文件摘要

namespace std {
    enum class future_errc {
        broken_promise,
        future_already_retrieved,
        promise_already_satisfied,
        no_state    
    };
    
    enum class launch : unspecified {
        async = unspecified,
        deferred = unspecified,
        implementation-defined
    };
    
    enum class future_status {
        ready,
        timeout,
        deferred
    };
    
    template <> struct is_error_code_enum<future_errc> : public true_type { };
    error_code make_error_code(future_errc e);
    error_condition make_error_condition(future_errc e);

    const error_category& future_category();

    class future_error;

    template <class R> class promise;
    template <class R> class promise<R&>;
    template <> class promise<void>;

    template <class R>
    void swap(promise<R>& x, promise<R>& y);

    template <class R, class Alloc>
    struct uses_allocator<promise<R>, Alloc>;

    template <class R> class future;
    template <class R> class future<R&>;
    template <> class future<void>;
    
    template <class R> class shared_future;
    template <class R> class shared_future<R&>;
    template <> class shared_future<void>;

    template <class> class packaged_task; // undefined
    template <class R, class... ArgTypes>
    class packaged_task<R(ArgTypes...)>;

    template <class R>
    void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&);

    template <class R, class Alloc>
    struct uses_allocator<packaged_task<R>, Alloc>;

    template <class F, class... Args>
    future<typename result_of<F(Args...)>::type>
    async(F&& f, Args&&... args);

    template <class F, class... Args>
    future<typename result_of<F(Args...)>::type>
    async(launch policy, F&& f, Args&&... args);
}

6.1.2 std::future_error 类摘要

namespace std {
    class future_error : public logic_error {
        public:
            future_error(error_code ec); // exposition only
            const error_code& code() const noexcept;
            const char* what() const noexcept;
    };
}

6.1.3 std::promise 类摘要

namespace std {
    template <class R>
    class promise {
        public:
            promise();
            template <class Allocator>
            promise(allocator_arg_t, const Allocator& a);
            promise(promise&& rhs) noexcept;
            promise(const promise& rhs) = delete;
            ~promise();

            // assignment
            promise& operator=(promise&& rhs) noexcept;
            promise& operator=(const promise& rhs) = delete;

            void swap(promise& other) noexcept;

            // retrieving the result
            future<R> get_future();

            // setting the result
            void set_value(see below);
            void set_exception(exception_ptr p);

            // setting the result with deferred notification
            void set_value_at_thread_exit(const R& r);
            void set_value_at_thread_exit(see below);
            void set_exception_at_thread_exit(exception_ptr p);
    };
    template <class R>
    void swap(promise<R>& x, promise<R>& y);
    template <class R, class Alloc>
    struct uses_allocator<promise<R>, Alloc>;
}

6.1.4 std::future 类摘要

namespace std {
    template <class R>
    class future {
        public:
            future();
            future(future &&);
            future(const future& rhs) = delete;
            ~future();

            future& operator=(const future& rhs) = delete;
            future& operator=(future&&) noexcept;
            shared_future<R> share() &&;

            // retrieving the value
            see below get();

            // functions to check state
            bool valid() const;
            void wait() const;
            template <class Rep, class Period>
            future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
            template <class Clock, class Duration>
            future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
    };
}

6.1.5 std::shared_future 类摘要

namespace std {
    template <class R>
    class shared_future {
        public:
            shared_future() noexcept;
            shared_future(const shared_future& rhs);
            shared_future(future<R>&&) noexcept;
            shared_future(shared_future&& rhs) noexcept;
            ~shared_future();

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

            // retrieving the value
            see below get() const;

            // functions to check state
            bool valid() const;
            void wait() const;
            template <class Rep, class Period>
            future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
            template <class Clock, class Duration>
            future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
    };
}

6.1.6 std::async 函数摘要

namespace std {
    template <class F, class... Args>
    future<typename result_of<F(Args...)>::type>
    async(F&& f, Args&&... args);

    template <class F, class... Args>
    future<typename result_of<F(Args...)>::type>
    async(launch policy, F&& f, Args&&... args);
}

6.1.7 std::packaged_task 类摘要

namespace std {
    template<class> class packaged_task; // undefined

    template<class R, class... ArgTypes>
    class packaged_task<R(ArgTypes...)> {
        public:
            typedef R result_type;

            // construction and destruction
            packaged_task() noexcept;
            template <class F>
            explicit packaged_task(F f);
            template <class F, class Allocator>
            explicit packaged_task(allocator_arg_t, const Allocator& a, F f);
            explicit packaged_task(R(*f)(ArgTypes...));
            template <class F>
            explicit packaged_task(F&& f);
            template <class F, class Allocator>
            explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
            ~packaged_task();

            // no copy
            packaged_task(packaged_task&) = delete;
            packaged_task& operator=(packaged_task&) = delete;

            // move support
            packaged_task(packaged_task&& other) noexcept;
            packaged_task& operator=(packaged_task&& other);
            void swap(packaged_task& other) noexcept;
            bool valid() const noexcept;

            // result retrieval
            future<R> get_future();
            
            // execution
            void operator()(ArgTypes... );
            void make_ready_at_thread_exit(ArgTypes...);
            void reset();
    };

    template <class R, class... ArgTypes>
    void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;
    template <class R, class Alloc>
    struct uses_allocator<packaged_task<R>, Alloc>;
}