C++11并发编程:多线程std::thread
一:概述
C++11引入了thread類,大大降低了多線程使用的復(fù)雜度,原先使用多線程只能用系統(tǒng)的API,無法解決跨平臺問題,一套代碼平臺移植,對應(yīng)多線程代碼也必須要修改。現(xiàn)在在C++11中只需使用語言層面的thread可以解決這個問題。
所需頭文件<thread>
二:構(gòu)造函數(shù)
1.默認(rèn)構(gòu)造函數(shù)
thread() noexcept
一個空的std::thread執(zhí)行對象
2.初始化構(gòu)造函數(shù)
template<class Fn, class... Args>
explicit thread(Fn&& fn, Args&&... args);
創(chuàng)建std::thread執(zhí)行對象,線程調(diào)用threadFun函數(shù),函數(shù)參數(shù)為args。
3.拷貝構(gòu)造函數(shù)
thread(const thread&) = delete;
拷貝構(gòu)造函數(shù)被禁用,std::thread對象不可拷貝構(gòu)造
4.Move構(gòu)造函數(shù)
thread(thread&& x)noexcept
調(diào)用成功原來x不再是std::thread對象
三:成員函數(shù)
1.get_id()
獲取線程ID,返回類型std::thread::id對象。
1 thread t1(threadFun); 2 thread::id threadId = t1.get_id(); 3 cout << "線程ID:" << threadId << endl; 4 5 //threadId轉(zhuǎn)換成整形值,所需頭文件<sstream> 6 ostringstream oss; 7 oss << t1.get_id(); 8 string strId = oss.str(); 9 unsigned long long tid = stoull(strId); 10 cout << "線程ID:" << tid << endl;2.join()
創(chuàng)建線程執(zhí)行線程函數(shù),調(diào)用該函數(shù)會阻塞當(dāng)前線程,直到線程執(zhí)行完join才返回。
thread t1(threadFun); t1.join() //阻塞等待3.detach()
detach調(diào)用之后,目標(biāo)線程就成為了守護(hù)線程,駐留后臺運(yùn)行,與之關(guān)聯(lián)的std::thread對象失去對目標(biāo)線程的關(guān)聯(lián),無法再通過std::thread對象取得該線程的控制權(quán)。
4.swap()
交換兩個線程對象
1 thread t1(threadFun1); 2 thread t2(threadFun2); 3 cout << "線程1的ID:" << t1.get_id() << endl; 4 cout << "線程2的ID:" << t2.get_id() << endl; 5 6 t1.swap(t2); 7 8 cout << "線程1的ID:" << t1.get_id() << endl; 9 cout << "線程2的ID:" << t2.get_id() << endl;5.hardware_concurrency()
獲得邏輯處理器儲量,返回值為int型
?1 int coreNum = thread::hardware_concurrency();?
四:使用
?1.創(chuàng)建線程
1 void threadFun1() 2 { 3 cout << "this is thread fun1 !" << endl; 4 } 5 6 int main() 7 { 8 thread t1(threadFun1); 9 t1.join(); 10 11 getchar(); 12 return 1; 13 }2.創(chuàng)建線程,傳參
1 void threadFun1(int v) 2 { 3 cout << "this is thread fun1 !" << endl; 4 cout << v << endl; 5 } 6 7 int main() 8 { 9 int value = 6; 10 thread t1(threadFun1, value); 11 t1.join(); 12 13 getchar(); 14 return 1; 15 }需要注意,變量int value 和int v 做變量傳遞時并不是引用,而是對變量做了拷貝,所以在傳遞給int v前,int value不能出作用域(釋放了內(nèi)存),join(),可以保證int value變量釋放內(nèi)存,如果使用detach(),可能存在這種情況。
3.創(chuàng)建線程,引用傳參
1 void threadFun1(int& v) 2 { 3 cout << "this is thread fun1 !" << endl; 4 cout << v << endl; 5 } 6 7 int main() 8 { 9 int value = 6; 10 thread t1(threadFun1, std::ref(value)); 11 t1.join(); 12 13 getchar(); 14 return 1; 15 }4.創(chuàng)建建線程,線程函數(shù)為類成員函數(shù)
1 class Object 2 { 3 public: 4 Object() 5 { 6 cout << "構(gòu)造函數(shù)" << endl; 7 } 8 9 ~Object() 10 { 11 cout << "析構(gòu)函數(shù)" << endl; 12 } 13 14 void fun(string info) 15 { 16 cout << info << endl; 17 } 18 19 }; 20 21 int main() 22 { 23 24 Object obj; 25 string str = "我是一個類的成員函數(shù)!"; 26 thread t1(&Object::fun, &obj, str); 27 t1.join(); 28 29 getchar(); 30 return 1; 31 }?
掃碼關(guān)注公眾號
專注分享C/C++,C++(11,14,17),STL,Java,Spring,mybatis,mysql,redis,分布式,高并發(fā),設(shè)計模式,爬蟲,docker,shell編程等相關(guān)技術(shù),還有高薪互聯(lián)網(wǎng)職位內(nèi)推,在這里一起探討,一起學(xué)習(xí),一起進(jìn)步,同時不定期分享視頻書籍資源,充分利用碎片化時間,讓我們的技術(shù)之路更加有樂趣!
轉(zhuǎn)載于:https://www.cnblogs.com/woniu201/p/10145044.html
總結(jié)
以上是生活随笔為你收集整理的C++11并发编程:多线程std::thread的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CF手游怎么改名字
- 下一篇: POJ_3262 Protecting