開(kāi)篇
多線程是開(kāi)發(fā)中必不可少的,往往我們需要多個(gè)任務(wù)并行,就需要多線程開(kāi)發(fā);就好比圖像檢測(cè)和圖像結(jié)果的處理,這就是一個(gè)可閉環(huán)的任務(wù),用多線程是可以加速這個(gè)任務(wù)的;
線程的狀態(tài)
就緒態(tài):線程能夠運(yùn)行,正在等待處理機(jī)資源;
運(yùn)行態(tài):正在運(yùn)行,可能有多個(gè)線程處于運(yùn)行態(tài);
阻塞態(tài):線程由于等待某些條件而無(wú)法運(yùn)行,例如IO、鎖、互斥量等;
終止態(tài):線程從起始函數(shù)返回或被取消;
多線程的構(gòu)建
有三種方式可以構(gòu)建多線程,前提是都需要引入pthread.h這個(gè)頭文件;
1、函數(shù);
2、仿函數(shù);
3、Lambda表達(dá)式;
三者的本質(zhì)都是在調(diào)用函數(shù);
1
2
3
4
5
6
7
8
9
10
11
|
// 函數(shù)方式 void fun(string s){ cout<< &s<<endl; cout<< "first thread programm" <<s<<endl; } int main(){ string s = "Hell world" ; thread th = thread (fun, s); th.join(); } |
上面代碼為最簡(jiǎn)單線程的一個(gè)構(gòu)造;
join函數(shù)是一個(gè)等待線程完成函數(shù),主線程需要等待子線程運(yùn)行結(jié)束才可以結(jié)束;還有一個(gè)detach的函數(shù),會(huì)讓線程在后臺(tái)運(yùn)行,需要等到程序退出才結(jié)束;
計(jì)算時(shí)間
計(jì)算時(shí)間在這里介紹兩種方式:
一、程序運(yùn)行時(shí)間
1
2
3
4
5
6
7
8
|
long n =0; clock_t start,finish; start= clock (); while (n<1000000000) n++; finish= clock (); printf ( "spend time %f s " , ( double )(finish-start)/CLOCKS_PER_SEC); printf ( "spend time %f ms " , ( double )(finish-start)/1000); |
這種方式和系統(tǒng)時(shí)間無(wú)關(guān),一般用來(lái)調(diào)試時(shí)打印時(shí)間;
二、chrono
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <chrono> //方式三 chrono std::chrono::system_clock::time_point Cstart = std::chrono::system_clock::now(); //系統(tǒng)時(shí)間 // std::chrono::steady_clock::time_point Cstart = std::chrono::steady_clock::now(); //穩(wěn)定時(shí)間 long n =0 ; while (n<1000000000)n++; std::chrono::system_clock::time_point Cend = std::chrono::system_clock::now(); //系統(tǒng)時(shí)間 std::chrono::duration< float > spend_time = Cend-Cstart; cout<<spend_time.count()<<endl; |
這個(gè)方式用系統(tǒng)時(shí)間進(jìn)行計(jì)算,在實(shí)際程序中用這個(gè)方式;
共享資源和互斥鎖
關(guān)于互斥鎖的概念,引用這篇博主的講解:文章
引入互斥鎖原因:當(dāng)有兩個(gè)線程共享一塊資源時(shí),容易造成沖突,也就是上個(gè)線程還沒(méi)結(jié)束就進(jìn)行下個(gè)線程,舉個(gè)例子就是讀寫(xiě)操作,添加互斥鎖可以很好的解決這個(gè)沖突問(wèn)題;
互斥鎖是個(gè)簡(jiǎn)單的加鎖方法,互斥鎖只有兩種狀態(tài):上鎖(lock)和解鎖(unlock);
互斥鎖特點(diǎn):
1、原子性:把一個(gè)互斥量鎖定為一個(gè)原子操作,這意味著如果一個(gè)線程鎖定了一個(gè)互斥量,沒(méi)有其他線程在同一時(shí)間可以成功鎖定這個(gè)互斥量;
2、唯一性:如果一個(gè)線程鎖定了一個(gè)互斥量,在它解除鎖定之前,沒(méi)有其他線程可以鎖定這個(gè)互斥量;
3、非繁忙等待:如果一個(gè)線程已經(jīng)鎖定了一個(gè)互斥量,第二個(gè)線程又試圖去鎖定這個(gè)互斥量,則第二個(gè)線程將被掛起(不占用任何cpu資源),直到第一個(gè)線程解除對(duì)這個(gè)互斥量的鎖定為止,第二個(gè)線程則被喚醒并繼續(xù)執(zhí)行,同時(shí)鎖定這個(gè)互斥量。
互斥鎖的使用:
1
2
3
4
5
|
mutex mtx; //創(chuàng)建互斥鎖對(duì)象 mtx.lock(); g_pcm_elapseds.push_back(std::make_pair(pcm_data, elapsed)); // 執(zhí)行語(yǔ)句 mtx.unlock(); |
condition_variable
condition_variable條件變量可以阻塞(wait)調(diào)用的線程直到使用(notify_one或notify_all)通知恢復(fù)為止
使用案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
std::mutex mtx; std::condition_variable cv; bool ready = false ; void print_thread_id( int id){ std::unique_lock<std::mutex> lck(mtx); cv.wait(lck,[]{ return ready;}); std::cout<< "thread" <<id <<endl; } void go(){ std::unique_lock<std::mutex> lck(mtx); ready = true ; cv.notify_all(); // 喚醒所有線程 }; int main(){ std:: thread threads[10]; for ( int i=0;i<10;i++){ threads[i] = std:: thread (print_thread_id,i); } std::cout<< " thread read all done" <<endl; go(); for (auto &th:threads) th.join(); return 0; } |
線程池
作用:每一個(gè)任務(wù)都起一個(gè)線程,這樣的效率是不高的,起一個(gè)線程池,哪個(gè)線程空閑就來(lái)處理任務(wù),這樣的結(jié)構(gòu)高效;
實(shí)現(xiàn)思想:管理一個(gè)任務(wù)隊(duì)列,一個(gè)線程隊(duì)列,然后每次取一個(gè)任務(wù)隊(duì)列分配給一個(gè)線程去做,循環(huán)反復(fù);
這里參考一個(gè)Github:地址
其中的ThreadPool.h頭文件寫(xiě)的很好,可以直接使用;
總結(jié)
線程這部分涉及的知識(shí)點(diǎn)比較多,實(shí)現(xiàn)起來(lái)細(xì)節(jié)也多。本篇先對(duì)其中的概念部分進(jìn)行總結(jié),實(shí)戰(zhàn)代碼部分可參考我提供的文章進(jìn)行學(xué)習(xí)。后續(xù)有精力會(huì)更新在線程的實(shí)戰(zhàn),想要掌握線程還是需要從實(shí)戰(zhàn)中學(xué)習(xí)。
到此這篇關(guān)于C++學(xué)習(xí)之線程詳解的文章就介紹到這了,更多相關(guān)C++ 線程內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_40620310/article/details/121834841