1 #include <future> 2 #include<iostream> 3 #include <thread> 4 #include <thread> 5 #include <string> 6 using namespace std; 7 8 //全局通信变量 9 promise<string> val; 10 11 void main() 12 { 13 auto fun1 = []() 14 { 15 //一直等待,获取全局变量的值 16 future<string> fu = val.get_future(); 17 cout << "Waitting" << endl; 18 cout <<"获取线程消息" << fu.get() << endl; 19 }; 20 21 auto fun2 = []() 22 { 23 system("pause"); 24 //val.set_value("hello"); 25 string str1; 26 cin >> str1; 27 val.set_value(str1); 28 system("pause"); 29 }; 30 31 thread t1(fun1); 32 thread t2(fun2); 33 34 t1.join(); 35 t2.join(); 36 system("pause"); 37 }