#include<iostream>#include<thread>usingnamespace std;voidThreadFunc(int&a){
// 引用传递
cout <<"this is thread func"<< endl;
cout <<"a = "<<(a +=10)<< endl;// 20}intmain(){
int x =10;
thread t1(ThreadFunc, std::ref(x));
thread t2(std::move(t1));// t1 线程失去所有权
thread t3;
t3 = std::move(t2);// t2 线程失去所有权
t3.join();
cout <<"Main End"<<" x = "<< x << endl;// 20return0;}
主要成员函数。
get_id() ,获取线程 ID,返回类型 std::thread::id 对象。
std::thread::id main_thread_id = std::this_thread::get_id();voidis_main_thread(){
if(main_thread_id == std::this_thread::get_id())
std::cout <<"This is the main thread.\n";else
std::cout <<"This is not the main thread.\n";}intmain(){
is_main_thread();
std::thread th(is_main_thread);
th.join();return0;}