c++11&14-多線程要點匯總
在C++11以前,C++的多線程編程均需依賴系統(tǒng)或第三方接口實現(xiàn),一定程度上影響了代碼的移植性。C++11中,引入了boost庫中的多線程部分內(nèi)容,形成C++標準,形成標準后的boost多線程編程部分接口基本沒有變化,這樣方便了以前使用boost接口開發(fā)的使用者切換使用C++標準接口,很容易把boost接口升級為C++標準接口。
我們通過如下幾部分介紹C++11多線程方面的接口及使用方法。
1. std::thread
std::thread為C++11的線程類,使用方法和boost接口一樣,非常方便,同時,C++11的std::thread解決了boost::thread中構成參數(shù)限制的問題,我想這都是得益于C++11的可變參數(shù)的設計風格。
我們通過如下代碼熟悉下std::thread使用風格:
//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; }
注意編譯時要使用:g++ c11.cpp -lpthread
運行結果:
threadfun1 - 1
threadfun2 - 1
threadfun1 - 2
join
detach
2. std::atomic
std::atomic為C++11封裝的原子數(shù)據(jù)類型。
什么是原子數(shù)據(jù)類型?從功能上看,簡單地說,原子數(shù)據(jù)類型不會發(fā)生數(shù)據(jù)競爭,能直接用在多線程中而不必我們用戶對其進行添加互斥資源鎖的類型。從實現(xiàn)上來看,我們可以理解為這些原子類型內(nèi)部自己加了鎖。
我們下面通過一個測試例子說明原子類型std::atomic的特點。
我們使用10個線程,把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(); } }
運行結果:
iCount:10
iCount:9
iCount:8
iCount:7
iCount:6
iCount:5
iCount:4
iCount:3
iCount:2
iCount:1
從上面的結果可以看到,iCount的最小結果是1,沒有出現(xiàn)小于等于0的情況,大家可以把iCount改成100甚至1000看看,可能會更直觀一點。
3. std::condition_variable
C++11中的std::condition_variable就像Linux下使用pthread_cond_wait和pthread_cond_signal一樣,可以讓線程休眠,直到被喚醒,然后再重新執(zhí)行。線程等待在多線程編程中使用非常頻繁,經(jīng)常需要等待一些異步執(zhí)行的條件的返回結果。
代碼如下:
#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); //線程將進入休眠 // ... 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; }
運行結果:
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
上面的代碼,在調用go函數(shù)之前,10個線程都處于休眠狀態(tài),當cv.notify_all()
運行后,線程休眠結束,繼續(xù)往下運行,最終輸出如上結果。
以上就是c++11&14-多線程知識匯總的詳細內(nèi)容,更多關于c++11&14 多線程使用的資料請關注腳本之家其它相關文章!
相關文章
C++實現(xiàn)LeetCode(199.二叉樹的右側視圖)
這篇文章主要介紹了C++實現(xiàn)LeetCode(199.二叉樹的右側視圖),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08Inline Hook(ring3)的簡單C++實現(xiàn)方法
這篇文章主要介紹了Inline Hook(ring3)的簡單C++實現(xiàn)方法,需要的朋友可以參考下2014-08-08C語言輸出旋轉后數(shù)組中的最小數(shù)元素的算法原理與實例
這篇文章主要介紹了C語言輸出旋轉后數(shù)組中的最小數(shù)元素的算法原理與實例,數(shù)組旋轉就是把開頭的幾個指定的元素放到數(shù)組的末尾,需要的朋友可以參考下2016-03-03C++實現(xiàn)LeetCode(33.在旋轉有序數(shù)組中搜索)
這篇文章主要介紹了C++實現(xiàn)LeetCode(33.在旋轉有序數(shù)組中搜索),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07