1#include <future>//process Communication for future results2#include <iostream>3#include <thread>4#include <string>5#include <chrono>//Time6#include <mutex>//Mutex Amount7 using namespacestd;8 9 //Create Mutex AmountTen Mutex m; One A //Global Communication Variables -promise<string>Val; - the voidMain () - { - //accessing external variables [=] -Auto fun = [=] (intIndex)int + { - //Locking +Lock_guard<mutex>lckg (m); A at //Show thread ID -cout <<"Thread Number:"<< this_thread::get_id () <<" "<< Index <<Endl; - //wait 10 seconds -This_thread::sleep_for (Chrono::seconds (1)); - - //Calculation in returnIndex *1024x768; - }; to + //get the return value, create a task package -packaged_task<int(int) >pt1 (fun); thepackaged_task<int(int) >pt2 (fun); * $Thread T1 ([&]() Panax Notoginseng { -PT1 ( at); the }); +Thread T2 ([&]() A { thePT2 ( -); + }); - $ //open Thread, get results, get only once $ intRes1 = Pt1.get_future ().Get(); - intRes2 = Pt2.get_future ().Get(); - intLast = Res1 +Res2; thecout << Last <<Endl; - Wuyi t1.joinable (); the t2.joinable (); - T1.join (); Wu T2.join (); -System"Pause"); About}
2. Task Pack Multithreading Parallel Computing