c++11&14-多線程要點(diǎn)匯總
在C++11以前,C++的多線程編程均需依賴系統(tǒng)或第三方接口實(shí)現(xiàn),一定程度上影響了代碼的移植性。C++11中,引入了boost庫中的多線程部分內(nèi)容,形成C++標(biāo)準(zhǔn),形成標(biāo)準(zhǔn)后的boost多線程編程部分接口基本沒有變化,這樣方便了以前使用boost接口開發(fā)的使用者切換使用C++標(biāo)準(zhǔn)接口,很容易把boost接口升級(jí)為C++標(biāo)準(zhǔn)接口。
我們通過如下幾部分介紹C++11多線程方面的接口及使用方法。
1. std::thread
std::thread為C++11的線程類,使用方法和boost接口一樣,非常方便,同時(shí),C++11的std::thread解決了boost::thread中構(gòu)成參數(shù)限制的問題,我想這都是得益于C++11的可變參數(shù)的設(shè)計(jì)風(fēng)格。
我們通過如下代碼熟悉下std::thread使用風(fēng)格:
//c11.cpp #include <iostream> #include <thread> void threadfun1() { std::cout << "threadfun1 - 1\r\n" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "threadfun1 - 2" << std::endl; } void threadfun2(int iParam, std::string sParam) { std::cout << "threadfun2 - 1" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(5)); std::cout << "threadfun2 - 2" << std::endl; } int main() { std::thread t1(threadfun1); std::thread t2(threadfun2, 10, "abc"); t1.join(); std::cout << "join" << std::endl; t2.detach(); std::cout << "detach" << std::endl; }
注意編譯時(shí)要使用:g++ c11.cpp -lpthread
運(yùn)行結(jié)果:
threadfun1 - 1
threadfun2 - 1
threadfun1 - 2
join
detach
2. std::atomic
std::atomic為C++11封裝的原子數(shù)據(jù)類型。
什么是原子數(shù)據(jù)類型?從功能上看,簡(jiǎn)單地說,原子數(shù)據(jù)類型不會(huì)發(fā)生數(shù)據(jù)競(jìng)爭(zhēng),能直接用在多線程中而不必我們用戶對(duì)其進(jìn)行添加互斥資源鎖的類型。從實(shí)現(xiàn)上來看,我們可以理解為這些原子類型內(nèi)部自己加了鎖。
我們下面通過一個(gè)測(cè)試?yán)诱f明原子類型std::atomic的特點(diǎn)。
我們使用10個(gè)線程,把std::atomic類型的變量iCount從10減到1。
//c11.cpp #include <thread> #include <atomic> #include <stdio.h> #include <iostream> #include <list> std::atomic<bool> bIsReady(false); std::atomic<int> iCount(10); void threadfun1() { if (!bIsReady) { std::this_thread::yield(); } while (iCount > 0) { printf("iCount:%d\r\n", iCount--); } } int main() { std::list<std::thread> lstThread; for (int i = 0; i < 10; ++i) { lstThread.push_back(std::thread(threadfun1)); } for (auto& th : lstThread) { th.join(); } }
運(yùn)行結(jié)果:
iCount:10
iCount:9
iCount:8
iCount:7
iCount:6
iCount:5
iCount:4
iCount:3
iCount:2
iCount:1
從上面的結(jié)果可以看到,iCount的最小結(jié)果是1,沒有出現(xiàn)小于等于0的情況,大家可以把iCount改成100甚至1000看看,可能會(huì)更直觀一點(diǎn)。
3. std::condition_variable
C++11中的std::condition_variable就像Linux下使用pthread_cond_wait和pthread_cond_signal一樣,可以讓線程休眠,直到被喚醒,然后再重新執(zhí)行。線程等待在多線程編程中使用非常頻繁,經(jīng)常需要等待一些異步執(zhí)行的條件的返回結(jié)果。
代碼如下:
#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> // std::mutex, std::unique_lock #include <condition_variable> // std::condition_variable std::mutex mtx; std::condition_variable cv; bool ready = false; void print_id(int id) { std::unique_lock<std::mutex> lck(mtx); while (!ready) cv.wait(lck); //線程將進(jìn)入休眠 // ... std::cout << "thread " << id << '\n'; } void go() { std::unique_lock<std::mutex> lck(mtx); ready = true; cv.notify_all(); } int main() { std::thread threads[10]; // spawn 10 threads: for (int i = 0; i<10; ++i) threads[i] = std::thread(print_id, i); std::cout << "10 threads ready to race...\n"; go(); // go! for (auto& th : threads) th.join(); return 0; }
運(yùn)行結(jié)果:
10 threads ready to race...
thread 0
thread 1
thread 2
thread 3
thread 4
thread 5
thread 6
thread 7
thread 8
thread 9
上面的代碼,在調(diào)用go函數(shù)之前,10個(gè)線程都處于休眠狀態(tài),當(dāng)cv.notify_all()
運(yùn)行后,線程休眠結(jié)束,繼續(xù)往下運(yùn)行,最終輸出如上結(jié)果。
以上就是c++11&14-多線程知識(shí)匯總的詳細(xì)內(nèi)容,更多關(guān)于c++11&14 多線程使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- c++ 端口掃描程序?qū)崿F(xiàn)案例
- c++11 多線程編程——如何實(shí)現(xiàn)線程安全隊(duì)列
- c++11多線程編程之std::async的介紹與實(shí)例
- C++11中多線程編程-std::async的深入講解
- C++11 簡(jiǎn)單實(shí)現(xiàn)線程池的方法
- 詳解C++11 線程休眠函數(shù)
- c++11新特性多線程操作實(shí)戰(zhàn)
- C++11用兩個(gè)線程輪流打印整數(shù)的實(shí)現(xiàn)方法
- C++多線程獲取返回值方法詳解
- 基于C++11的threadpool線程池(簡(jiǎn)潔且可以帶任意多的參數(shù))
- c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽多個(gè)端口
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(199.二叉樹的右側(cè)視圖)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(199.二叉樹的右側(cè)視圖),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Inline Hook(ring3)的簡(jiǎn)單C++實(shí)現(xiàn)方法
這篇文章主要介紹了Inline Hook(ring3)的簡(jiǎn)單C++實(shí)現(xiàn)方法,需要的朋友可以參考下2014-08-08從頭學(xué)習(xí)C語言之for語句和循環(huán)嵌套
這篇文章主要為大家詳細(xì)介紹了C語言之for語句和循環(huán)嵌套,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01C語言輸出旋轉(zhuǎn)后數(shù)組中的最小數(shù)元素的算法原理與實(shí)例
這篇文章主要介紹了C語言輸出旋轉(zhuǎn)后數(shù)組中的最小數(shù)元素的算法原理與實(shí)例,數(shù)組旋轉(zhuǎn)就是把開頭的幾個(gè)指定的元素放到數(shù)組的末尾,需要的朋友可以參考下2016-03-03C++實(shí)現(xiàn)LeetCode(33.在旋轉(zhuǎn)有序數(shù)組中搜索)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(33.在旋轉(zhuǎn)有序數(shù)組中搜索),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07