目录
- future模板类
- promise类
- packaged_task模板类
- async模板函数
- shared_future模板类
标准库提供了一些工具来获取异步任务(即在单独的线程中启动的函数)的返回值,并捕捉其所抛出的异常。这些值在共享状态中传递,其中异步任务可以写入其返回值或存储异常,而且可以由持有该引用该共享态的 std::future 或 std::shared_future 实例的线程检验、等待或是操作这个状态。
并发支持库 (C++11 起) - cppreference.com
future模板类
概念:
future - C++ Reference (cplusplus.com)
future类用于给线程提供一个方便管理和获取异步操作的结果的功能.
这个类允许将一个异步操作封装为一个未来可获取结果的对象,从而实现了高效的并发编程。
注意:
- future本身没有set方法,必须搭配promise进行使用.
- future对象要在需要传递结果的线程间可见(能够看见同一个future) -- 引用传递
成员函数:
- template< class T > class future;
- template< class T > class future<T&>;
- template<> class future<void>; //空 -- 不需要返回结果的future.
复制代码- future() noexcept; //空对象
- future( future&& other ) noexcept; //只允许移动
- future( const future& other ) = delete; //禁止拷贝
复制代码- future& operator=( future&& other ) noexcept; //只允许移动
- future& operator=( const future& other ) = delete; //禁止拷贝
复制代码 取出future对象内保存的数据.是阻塞函数,如果目标future所在线程一直没有set_value,则会一直等待,直到set_value,成功获取到数据后解除阻塞.- //三个重载函数,根据future的数据类型自动决定
- T get();
- T& get();
- void get(); //空future对象没有东西传的时候使用
复制代码 阻塞等待,直到目标线程set_value.和get类似,只是没有返回值.阻塞等待一段时长和等待到某时刻.之后会返回future的状态- template< class Rep, class Period >
- std::future_status wait_for( const std::chrono::duration<Rep,Period>& timeout_duration ) const;
- template< class Clock, class Duration >
- std::future_status wait_until( const std::chrono::time_point<Clock,Duration>& timeout_time ) const;
复制代码 常量解释future_status::deferred子线程中的任务函仍未启动(配合std::async生效)future_status::ready子线程中的任务已经执行完毕,结果已就绪future_status::timeout子线程中的任务正在执行中,指定等待时长已用完
返回多个线程共享的future对象,即shared_future模板类- shared_future<T> share();
复制代码 返回值:shared_future(std::move(*this))
promise类
promise类内封装了future对象,用于管理和使用future对象.
- template< class R > class promise;
- template< class R > class promise<R&>;
- template<> class promise<void>;
复制代码- promise();
- promise( promise&& other ) noexcept; //
- promise( const promise& other ) = delete; //禁止拷贝
复制代码 在std::promise类内部管理着一个future类对象,调用get_future()就可以得到这个future对象了- std::future<T> get_future();
复制代码 set_value设置结果为指定值 (公开成员函数)set_value_at_thread_exit设置结果为指定值,同时仅在线程退出时分发提醒 (公开成员函数)set_exception设置结果为指示异常 (公开成员函数)set_exception_at_thread_exit设置结果为指示异常,同时仅在线程退出时分发提醒 (公开成员函数)存储要传出的 value 值,并立即让状态就绪,这样数据被传出其它线程就可以得到这个数据了。重载的第四个函数是为promise类型的对象准备的。- void set_value( const R& value );
- void set_value( R&& value );
- void set_value( R& value );
- void set_value();
复制代码 存储要传出的 value 值,但是不立即令状态就绪。在当前线程退出时,子线程资源被销毁,再令状态就绪。- void set_value_at_thread_exit( const R& value );
- void set_value_at_thread_exit( R&& value );
- void set_value_at_thread_exit( R& value );
- void set_value_at_thread_exit();
复制代码 promise的使用例程:
[code]#include #include #include #include #include void func(std::promise& pro) { std::this_thread::sleep_for(std::chrono::seconds(1)); pro.set_value("hello world"); std::this_thread::sleep_for(std::chrono::seconds(1));}int main() { std::promise pro; std::thread t1(func, std::ref(pro)); auto fut = pro.get_future(); while (true) { bool flag = false; std::future_status ret = fut.wait_for(std::chrono::seconds(1)); switch (ret) { case std::future_status::deferred: //只有async函数才有效 std::cout |