packaged_task = promise + function
async = thread + packaged_task
future = promise->get_future();
shared_future = future->share();
11111111111111111111111111111111111111111111111
promise和future是线程之间的同步通道,类似于条件变量的分装
int main(){
std::promise<bool> prom;
//future有wait()和get()等方法,该方法会阻塞当前线程,直到future的源promise调用了set_value
//future的wait只有阻塞功能,而get()方法不仅有阻塞功能,还能拿到set_value设置的值
std::future<bool> f = prom.get_future();
prom.set_value(true);
std::cout << f.get() << std::endl;
}
2222222222222222222222222222222222222222222222222222222
int main(){
std::promise<int> prom;
auto f = prom.get_future();
std::thread t([](std::promise<int> p){
std::this_thread::sleep_for(2);
p.set_value(100);
},std::move(prom));
//这段代码执行后两秒输出100.验证了promise的future的get的阻塞和获取结果的能力
//一个promise的set_value只能调用一次,调用多次,会throw exception
//promise的阻塞功能还蛮好用的
std::cout << f.get() << std::endl;
if(t.joinable()) t.join();
}
222222222222222222222222.555555555555555555555555555
std::move使用前提:1,定义的类使用了资源并定义了移动构造函数和移动赋值运算符 2,该变量即将不再使用
// 移动构造函数
MyObject(MyObject&& other) noexcept : data(other.data) {
other.data = nullptr;
std::cout << "Move Constructor" << std::endl;
}
// 移动赋值操作符
MyObject& operator=(MyObject&& other) noexcept {
if (this != &other) {
delete data;
data = other.data;
other.data = nullptr;
}
std::cout << "Move Assignment Operator" << std::endl;
return *this;
}
3333333333333333333333333333333333333333333333333333333333333
介绍完promise,再看package_task
int main(){
std::packaged_task<int(int,int)> task([](int a,int b){return a+b;});
auto f = task.get_future();
std::thread t(std::move(task),1,2);
std::cout<<f.get()<<std::endl;
if(t.joinable()) t.join();
}
4444444444444444444444444444444444444444444444444444444444
在C++11中有个future头文件,在这个头文件有一个promise类模板,std::promise 提供存储值或异常的设施,之后通过 std::promise 对象所创建的 std::future 对象异步获得结果。其模板声明如下:
template< class R >
class promise;1) //空模板
template< class R >
class promise<R&>;//非 void 特化,用于在线程间交流对象
template<>
class promise<void>;//void 特化,用于交流无状态事件
每个 promise 与共享状态关联,共享状态含有一些状态信息和可能仍未求值的结果,非void特化类型的promise,其类型R就是线程间交流对象的类型;
promise/future一般是配对使用的,其中:
promise是 promise-future 交流通道的“推”端,它可以在某一时刻设置共享状态的值,通过promise的set系列函数进行设置。
future 对象可以异步返回共享状态的值,或者在必要的情况下阻塞调用者并等待共享状态标志变为 ready,然后才能获取共享状态的值。
示例
下面演示了通过promise/future对,进行线程间交流,一个线程用于设置共享状态,另一个线程用于获取共享状态结果,代码示例如下:
#include <iostream> // std::cout
#include <thread> // std::thread
#include <chrono>
#include <future>
#include <mutex>
using namespace std;
typedef struct
{
string strName;
string strAdd;
float fResult;
}STRU_STUDENT_INFO;
std::mutex g_mut;
//get operate
void get_val (std::promise<STRU_STUDENT_INFO>& promise)
{
this_thread::sleep_for(chrono::seconds(1));
try
{
//enter set_val thread;
g_mut.lock();
cout << "enter get thread: " << std::this_thread::get_id() << "\n";
g_mut.unlock();
//通过futur对象来获取共享状态中的值
future<STRU_STUDENT_INFO> fut = promise.get_future();
//阻塞获取,只能get 一次
STRU_STUDENT_INFO x = fut.get();
std::cout << "You entered " << " name :" << x.strName
<< "\taddr:" << x.strAdd
<< "\tresult: " << x.fResult << '\n';
}
catch (...)
{
std::cout << "[exception caught]";
}
}
//set operate
void set_val(std::promise<STRU_STUDENT_INFO>& pro)
{
this_thread::sleep_for(chrono::seconds(1));
STRU_STUDENT_INFO student;
student.strName = "jinxiang";
student.strAdd = "fuzhou";
student.fResult = 92.1;
//enter set_val thread;
g_mut.lock();
cout << "enter set thread: " << std::this_thread::get_id() << "\n";
g_mut.unlock();
//设置共享状态值,实现线程间状态交流
pro.set_value(student);
}
int main(int argc, char *argv[])
{
promise<STRU_STUDENT_INFO> promise;
//利用promise和future完成对线程间通信
thread th1(get_val, ref(promise));
thread th2(set_val, ref(promise));
th1.join();
th2.join();
return 0;
}
运行结果:
enter get thread: 140354130814720
enter set thread: 140354122422016
You entered name :jinxiang addr:fuzhou result: 92.1
promise不支持拷贝构造,只能进行移动操作,例如
promise<int> p1;
//ok
promise<int> p2 = std::move(p1);
//error
promise<int> p3 = p2;
总结
类模板promise通过promise/future对完成对共享状态的设置和获取,可用于线程间对象交流,对象的类型就是promise的模板参数。