在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的模板参数。
参考资料:
http://www.cnblogs.com/haippy/p/3239248.html
http://zh.cppreference.com/w/cpp/thread/promise