C + + Concurrent programming asynchronous task asynchronous task Std::async
(1) Std::async returns a Std::future object that holds the final computed result. When this value is required, only the Get () member function of the calling object is required. And the thread will not block until the expected state is ready; After that, the result of the calculation is returned.
std::future<int> answer =Std::async ([] {Std::stringstream stm; STM<<"Future ID:"<< std::this_thread::get_id () <<Std::endl; Std::cout<<Stm.str (); intsum =0; for(inti =1; I <= -; i++) {sum+=i; Std::cout<< I <<" "; } std::cout<<Std::endl; Std::this_thread::sleep_for (Std::chrono::seconds (1));//increased latency (1s) returnsum; }); Std::stringstream STM; STM<<"Main ID:"<< std::this_thread::get_id () <<Std::endl; Std::cout<<Stm.str (); Std::cout<<"answer is:"<< answer.Get() << Std::endl;//this will be blocked until the asynchronous task returnsStd::cout <<"Tag 2"<< Std::endl;
(2) Std::async allows additional parameters to be passed to the function by adding additional invocation parameters.
If the first argument is a pointer to a member function, the second argument provides a specific object with the function member class (either through a pointer or wrapped in std::ref), and the remaining arguments can be passed in as arguments to the member function.
Otherwise, all subsequent arguments are used as arguments to the function, or as the first parameter that specifies a callable object. For example Std::thread, when the parameter is an rvalue (rvalues), the copy operation transfers the original data using a move. This allows the "move only" type to be used as a function object and parameter.
classXfun { Public: Xfun () {}~Xfun () {}voidFConstSTD::string&str) {Std::stringstream stm; STM<<"F called."<< This<<" -"<< Str <<Std::endl; Std::cout<<Stm.str (); } std::stringGConstSTD::string&str) {Std::stringstream stm; STM<< Str <<"[--> Add by function g]"<< This; returnStm.str (); } //xfun& operator= (const xfun&) = delete; //xfun (const xfun&) = delete; void operator()(ConstSTD::string&str) {Std::stringstream stm; STM<<"operator () called."<< This<<" -"<< Str <<Std::endl; Std::cout<<Stm.str (); } }; Xfun x; Std::cout<<"addr of x:"<< &x <<Std::endl; Std::async (&xfun::f, &X, std::string("Test F ()")); Std::future<STD::string> F2 = Std::async (&xfun::g, X, std::string("Test g () Temp"));//creates a copy of an X object passed in, after removing the assignment function, will not compileStd::async (std::ref(x), std::string("test operator ()")); Std::async (Xfun (), std::string("test operator () temp"));//creates a Xfun temporary object that is passed inStd::cout << F2.Get() << Std::endl;
(3) By default, Std::async starts a new thread to complete the task, but can also specify additional execution methods:
Std::launch::d efered Specifies to wait until wait or get is called.
STD::LAUNCH::ASYNC specifies that it must be executed in a separate thread.
Default is: Std::launch::d efered | Std::launch::async
Auto F3 =Std::async (std::launch::d eferred, [] {Std::stringstream stm; STM<<"F3 called. TID:"<< std::this_thread::get_id () <<Std::endl; Std::cout<<Stm.str (); }); Auto F4=Std::async (Std::launch::async, [] {Std::stringstream stm; STM<<"F4 called. TID:"<< std::this_thread::get_id () <<Std::endl; Std::cout<<Stm.str (); }); Std::stringstream STM; STM<<"Main. TID:"<< std::this_thread::get_id () <<Std::endl; Std::cout<<Stm.str (); Std::this_thread::sleep_for (Std::chrono::seconds (1)); F3.wait ();
C + + Concurrent programming asynchronous task