基於qt的多執行緒流水線設計(4執行緒 主線程)

2021-08-06 04:26:04 字數 3016 閱讀 2325

這篇的**主體是參考

但是原文只用了主線程加上乙個子執行緒,而我設計所需架構需要4個子執行緒,並且是基於qt框架下的,所以結合qt多執行緒同步的知識,對之前的程式進行了改編。

補充一些qt多執行緒的小知識。

qthread 建立子執行緒,通過重寫qthread::run()函式,加入自己所需功能,利用qthread的物件呼叫start,開始執行

qmutex 互斥鎖,用於執行緒同步,主要需要用lock與unlock兩個上鎖解鎖功能

qwaitcondition 等待條件量,

——qwaitcondition::wait(&mutex) 等待mutex釋放,被喚醒,若鎖,則堵塞等待

——qwaitcondition::wakeone() 喚醒其他的乙個等待執行緒

——qwaitcondition::wakeall() 喚醒所有等待執行緒

struct stage_tag  stage1,stage2,stage3,stage4;

int i = 3

; //迴圈3次

/**

工人幹活的執行緒,基本迴圈:

1.等待前乾些準備工作;

2.等待輸入

3.處理輸入;

4.傳送訊號,解鎖

*/

void thread1::run()

//向工人2傳輸資料(此處傳輸功能都沒有寫)

emit getdata("tread1 processing...");

stage2.work_done = 0

;//表示工人2又有事做了(剛又來了資料)

stage2.work_data = 1

;//表示工人2有資料了,快來處理

stage2.has_data.wakeall();//通知工人2有資料了

stage2.mutex

.unlock();

}

}void thread2::run()

//thread2 處理資料傳給3,傳給3之後,喚醒1

stage3.mutex

.lock();

while (!stage3.work_done)

emit getdata("tread2 processing...");

stage3.work_done = 0

; stage3.work_data = 1

; stage3.has_data.wakeall();

stage3.mutex

.unlock();

//stage2.work_data = 0

; stage2.work_done = 1

; stage2.has_done.wakeall();

stage2.mutex

.unlock();

}}//以下類似

void thread3::run()

//thread3 處理資料傳給4,傳給4之後,喚醒2

stage4.mutex

.lock();

while (!stage4.work_done)

emit getdata("tread3 processing...");

stage4.work_done = 0

; stage4.work_data = 1

; stage4.has_data.wakeall();

stage4.mutex

.unlock();

//stage3.work_data = 0

; stage3.work_done = 1

; stage3.has_done.wakeall();

stage3.mutex

.unlock();

}}void thread4::run()

emit getdata("tread4 processing...");

stage4.work_data = 0

; stage4.work_done = 1

; stage4.has_done.wakeall();

stage4.mutex

.unlock();

i--;

}}

最後的輸出結果:可以看出是依次順序執行,相互等待的。不過最後乙個迴圈,工人4沒有工作,估計是i–命令的問題吧,切換之後,結束了程序

(因為資料處理基本沒花時間,看不出qwaitcondition 的功能,條件阻塞的效果)

tread2  waiting...

tread3 waiting...

tread1 processing...

tread1 waiting...

tread2 processing...

tread2 waiting...

tread3 processing...

tread3 waiting...

tread1 processing...

tread1 waiting...

tread4 processing...

tread4 waiting...

tread2 processing...

tread3 processing...

tread1 processing...

tread3 waiting...

tread4 processing...

tread2 processing...

tread4 waiting...

tread1 processing...

tread3 processing...

tread4 processing...

tread2 processing...

tread1 processing...

tread3 processing...

流水線暫停機制的設計

這是未新增流水線暫停的系統機構框圖,電腦繪圖的話有時候新增輸入輸出就要進行大改動,我就手畫了 在openmips流水線cpu設計中,乘累加 乘類減指令在流水線的執行階段占用多個時鐘週期,因此需要暫停流水線,等待這些多週期指令執行完成。實現流水線的暫停 只需要保持取指令位址pc的值不變。同一時候保持流...

流水線技術在高速數位電路設計中的應用

流水線技術在高速數位電路設計中的應用 譚欣 摘要 流水線技術是設計高速數位電路的一種最佳選擇之一,對其實現原理作了較形象的闡述。針對加法器在dsp中的重要作用,對流水線加法器中流水線技術的應用作了較深入的說明。同時,對流水線技術中引入暫存器事項也作了較全面的闡述。1 前言 數字訊號處理技術 dsp ...

基於Opencv的手勢識別(QT 多執行緒)

寒假閒來無事就搗騰下,先直接放效果圖 主要用opencv寫底層的演算法 並沒有用到機器學習 qt做的窗體。演算法部分 用的是hsv識別膚色,這種方法在亮度足夠的情況下對膚色識別還算ok,燈光的影響可以用白平衡解決 opencv的xphoto.hpp有具體的白平衡函式可以直接呼叫 但是這種方法有個明顯...