1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #include <thread> 4 #include <future> 5 #include <string> 6 #include <cstdlib> 7 using namespace std; 8 9 promise <string>val;//全局通信变量 10 void main() 11 { 12 //字符串相加 13 /*string str1("123"); 14 string str2("34"); 15 string str3(str1 + str2); 16 cout << str3 << endl;*/ 17 18 thread th1([]() 19 { 20 future<string> str = val.get_future();//获取未来状态 21 cout << "等待中" << endl; 22 //阻塞等待 23 cout << str.get() << endl; 24 } 25 ); 26 27 thread th2([]() 28 { 29 system("pause"); 30 val.set_value("hello"); 31 } 32 ); 33 34 th1.join(); 35 th2.join(); 36 cin.get(); 37 }