A, std :: async and std :: future usage
std :: async is a function template, std :: future is a class template
1 #include <iostream> 2 #include <thread> 3 #include <future> 4 #include <Windows.h> 5 6 using namespace std; 7 8 int mythread() 9 { 10 cout << "my thread:" << std::this_thread::get_id() << " start" << endl; 11 int temp = 0; 12 temp++; 13 Sleep(5000); 14 cout << "my thread:" << std::this_thread::get_id() << " end" << endl; 15 return temp; 16 } 17 int main() 18 { 19 cout << "main thread:" << std::this_thread::get_id() << " start" << endl; 20 std::future<int> result = std::async(std::launch::async, mythread); 21 //std::async(mythread);相当于std::async(std::launch::async, mythread); 22 //Automatically create a thread to begin executing thread entry function, and returns a std :: future objects to the Result 23 24- cout << the Result. GET () << endl; // waiting for entry function is executed, until I got the return value, namely the TEMP 25 // result.wait (); // just waiting for entry function executed, but does not return the results 26 is COUT << " main Thread: " << this_thread :: :: get_id STD () << " End " << endl; 27 System ( " PAUSE " ); 28 return 0 ; 29 }
std :: async (std :: launch :: async, mythread); if std :: async () The first parameter into std :: lauch :: deferred, then the thread will not be executed immediately, but to delay thread entry function of std :: future of wait () or get () function call if you are performing. In fact, it does not create
a child thread, but just call the entry function in the main function.
Two, std :: packaged_task usage
std :: packaged_task is a class template, template parameters can be called all kinds of objects, through which the various callable packaged, easy entry function as a thread to call
1 #include <iostream> 2 #include <thread> 3 #include <future> 4 #include <Windows.h> 5 6 using namespace std; 7 8 int fun(int val) 9 { 10 cout << "my thread:" << std::this_thread::get_id() << " start" << endl; 11 val++; 12 cout << "my thread:" << std::this_thread::get_id() << " end" << endl; 13 return val; 14 } 15 int main() 16 { 17 cout << "main thread:" << std::this_thread::get_id() << " start" << endl; 18 std::packaged_task<int(int)> pack(fun); 19 thread t1(std::ref(pack), 0); 20 t1.join(); 21 std::future<int> result = pack.get_future(); 22 cout << result.get() << endl; 23 cout << "main thread:" << std::this_thread::get_id() << " end" << endl; 24 system("pause"); 25 return 0; 26 }
Three, std :: promise of usage
std :: promise is a class template can be assigned to it in a thread, then in the other thread, at some point in the future, this value can be taken out.
1 #include <iostream> 2 #include <thread> 3 #include <future> 4 #include <Windows.h> 5 6 using namespace std; 7 8 void mythread1(std::promise<int> &pro, int val) 9 { 10 cout << "my thread1:" << std::this_thread::get_id() << " start" << endl; 11 val++; 12 val--; 13 Sleep(500); //假设运算了500毫秒 14 int result = val; 15 pro.set_value(val);//保存结果 16 cout << "my thread1:" << std::this_thread::get_id() << " end" << endl; 17 } 18 19 20 void mythread2(std::future<int> &getful) 21 { 22 cout << "my thread2:" << std::this_thread::get_id() << " start" << endl; 23 auto getval = getful.get(); 24 cout << getval << endl; 25 cout << "my thread2:" << std::this_thread::get_id() << " end" << endl; 26 } 27 int main() 28 { 29 cout << "main thread:" << std::this_thread::get_id() << " start" << endl; 30 std::promise<int> prom; //Declare a promise object type stored int 31 is Thread T1 (mythread1, STD :: REF (PROM), 10 ); 32 t1.join (); 33 is 34 is STD :: Future < int > prom.get_future FUL = (); // Promise and future binding, for returning the saved results 35 36 // Get results in the main thread 37 [ @ Auto ful.get result = (); // GET () only once, if the main thread is used a, t2 become unusable 38 is // COUT result << << endl; 39 40 // Get t2, results in the thread 41 is thread t2 (mythread2, STD :: REF (FUL)); 42 is t2.join (); 43 44 cout << "main thread:" << std::this_thread::get_id() << " end" << endl; 45 system("pause"); 46 return 0; 47 }
std :: ref value for the package passed by reference.