C++11 Language-level threading

Source: Internet
Author: User

C++11 Language-level threading

Creation of Threads

Creating a thread with Std::thread is as simple as providing a thread function or function object, and you can specify parameters for the thread function at the same time.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<chrono>#include<thread>voidfunc1 () { while(true) {std::this_thread::sleep_for (Std::chrono::milliseconds ( the)); Std::cout<< __function__ <<Std::endl; }}voidFunc2 () { while(true) {std::this_thread::sleep_for (Std::chrono::milliseconds ( the)); Std::cout<< __function__ <<Std::endl; }}voidFUNC3 (intACharChConst Char*str) {std::this_thread::sleep_for (Std::chrono::milliseconds ( the)); Std::cout<<"A ="<< a <<Std::endl; Std::cout<<"ch ="<< CH <<Std::endl; Std::cout<<"str ="<< Str <<Std::endl;}voidmytest () {std::thread T1 (func1);//Sub-thread 1Std::thread T2 (FUNC2);//Sub-thread 2//threads can also receive parameters for functionsstd::thread T3 (func3,1,'a',"XYZ");//Sub-thread 3std::thread T4 (func3,2,'a',"ABC");//Sub-thread 3     while(true)//Main Thread{std::this_thread::sleep_for (Std::chrono::milliseconds ( the)); Std::cout<< __function__ <<Std::endl; }    return;}intMain () {mytest (); System ("Pause"); return 0;}

Reclaim Thread Resources

Std::thread::join waits for the thread to end ( this function blocks ) and reclaims the thread resource, and if the thread function has a return value, the return value is ignored.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<chrono>#include<thread>voidPuase_thread (intNointN)    {std::this_thread::sleep_for (Std::chrono::seconds (n)); Std::cout<<"thread-"<< No <<"Pause of"<< N <<"seconds"<<Std::endl;}voidmytest () {//std::cout << "Spawning 3 threads ..." << Std::endl;Std::thread T1 (Puase_thread,1,3); Std::thread T2 (Puase_thread,2,3); std::thread T3 (Puase_thread,3,3); Std::cout<<"Done spawning threads. Now waiting-them to join:"<<Std::endl; T1.joinable ()? T1.join (): Std::cout <<"Thread id-"<< t1.get_id () <<"can not join"<<Std::endl; T2.joinable ()? T2.join (): Std::cout <<"Thread id-"<< t2.get_id () <<"can not join"<<Std::endl; T3.joinable ()? T3.join (): Std::cout <<"Thread id-"<< t3.get_id () <<"can not join"<<Std::endl; Std::cout<<"All Threads joined!"<<Std::endl; return;}intMain () {mytest (); System ("Pause"); return 0;}

If you do not want the thread to be blocked for execution, you can call the thread's Std::thread::d etach ( This function will not block ), detach the thread from the thread object, and let the thread execute as a background thread.

It is important to note, however, that detach cannot be contacted after a thread, such as detach, after which the join is no longer available to wait for execution, and when the thread finishes executing, we cannot control it.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<chrono>#include<thread>voidPuase_thread (intNointN)    {std::this_thread::sleep_for (Std::chrono::seconds (n)); Std::cout<<"thread-"<< No <<"Pause of"<< N <<"seconds"<<Std::endl;}voidmytest () {//std::cout << "Spawning 3 threads ..." << Std::endl;Std::thread T1 (Puase_thread,1,3); Std::thread T2 (Puase_thread,2,3); std::thread T3 (Puase_thread,3,3); Std::cout<<"Done spawning threads. Now waiting-them to join:"<<Std::endl; T1.joinable ()? T1.detach (): Std::cout <<"Thread id-"<< t1.get_id () <<"can not detach"<<Std::endl; T2.joinable ()? T2.detach (): Std::cout <<"Thread id-"<< t2.get_id () <<"can not detach"<<Std::endl; T3.joinable ()? T3.detach (): Std::cout <<"Thread id-"<< t3.get_id () <<"can not detach"<<Std::endl; Std::cout<<"All Threads joined!"<<Std::endl; return;}intMain () {mytest (); System ("Pause"); return 0;}

Gets the thread ID and CPU core number
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<chrono>#include<thread>voidPuase_thread (intN)    {std::this_thread::sleep_for (Std::chrono::seconds (n)); //std::this_thread::get_id () gets the thread IDStd::cout <<"thread-"<< std::this_thread::get_id () <<"Pause of"<< N <<"seconds"<<Std::endl;}voidmytest () {//std::cout << "Spawning 3 threads ..." << Std::endl;Std::thread T1 (Puase_thread,3); Std::thread T2 (Puase_thread,3); std::thread T3 (Puase_thread,3); Std::cout<<"Main ID:"<< std::this_thread::get_id () << Std::endl;//main thread IDStd::cout <<"CPU num:"<< std::thread::hardware_concurrency () << Std::endl;//gets the number of CPU cores, failed to return 0Std::cout<<"Done spawning threads. Now waiting-them to join:"<<Std::endl; //t1.get_id ()//gets T1 The ID of this threadT1.joinable ()? T1.detach (): Std::cout <<"Thread id-"<< t1.get_id () <<"can not detach"<<Std::endl; T2.joinable ()? T2.join (): Std::cout <<"Thread id-"<< t2.get_id () <<"can not join"<<Std::endl; T3.joinable ()? T3.join (): Std::cout <<"Thread id-"<< t3.get_id () <<"can not join"<<Std::endl; Std::cout<<"All Threads joined!"<<Std::endl; return;}intMain () {mytest (); System ("Pause"); return 0;}

C++11 Language-level threading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.