Starting with C + + 11, the language core and the standard library began to introduce native support for multithreading. As shown below:
intDosth (Charc) {default_random_engine Dre (c); Uniform_int_distribution<int> ID (Ten,10000); for(intI=0;i<Ten; i++) {this_thread::sleep_for (Chrono::milliseconds (DRE)); Cout.put (c). Flush (); } returnC;}intfunc1 () {returnDosth (',');}intFunc2 () {returnDosth ('+');}int_tmain (intARGC, _tchar*argv[]) {cout<<"func1 () background, FUNC2 () foreground."<<Endl; Future<int>result1 (Async (func1)); intRESULT2 =Func2 (); cout<<"result2 First return ="<< result2 <<Endl; intresult = Result1.Get() +result2; cout<<"end of asynchronous test."<<Endl;}
The output is as follows:
Func1 () background, FUNC2 () foreground.++,+,,+,+,+,+,,+,+,+result2 First returns = 43 The end of the asynchronous test.
As an introductory lesson, C + + standard library The second edition of the 18th chapter is still good.
Introduction to the C + + standard library multithreading