std::promise用于多线程间交换数据:
void test1(std::promise<int> &p)
{
//while (1)
{
std::cout<< "test1";
std::chrono::system_clock t;
std::cout << std::this_thread::get_id() << endl;
std::cout<< "test1 quit" <<endl;
int val = 123;
cout << "in data" << endl;
p.set_value(val);
p.set_value(val);
}
}
void test2(std::future<int>& f)
{
//while (1)
{
auto val = f.get();
cout << "val value:" << val;
std::cout<< "test2";
std::this_thread::sleep_for(seconds(3));
std::cout<< "test2 quit"<<endl;
}
}
int main()
{
std::promise<int> m;
std::future<int> n;
n = m.get_future();
thread testA(test1, std::ref(m));
thread testB(test2, std::ref(n));
testA.join();
testB.join();
cout << "Hello World end!" << endl;
return 0;
}