您好,登錄后才能下訂單哦!
這篇文章主要總結(jié)c++11&14-多線程要點(diǎn),內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
在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接口升級為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ù)類型?從功能上看,簡單地說,原子數(shù)據(jù)類型不會發(fā)生數(shù)據(jù)競爭,能直接用在多線程中而不必我們用戶對其進(jìn)行添加互斥資源鎖的類型。從實(shí)現(xiàn)上來看,我們可以理解為這些原子類型內(nèi)部自己加了鎖。
我們下面通過一個(gè)測試?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看看,可能會更直觀一點(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é)果。
以上就是關(guān)于總結(jié)c++11&14-多線程要點(diǎn)的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。