1. 程式人生 > >1.future線程通信

1.future線程通信

[] div pause HR log promise 全局變量 IT cout

 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 }

1.future線程通信