linux 多執行緒訊號量 盤子水果問題

2021-10-01 05:47:31 字數 3667 閱讀 2772

#include

#include

#include

#include

#include

#include

typedef

struct

frt_sem_mtx_def;

/* 父親執行緒:盤子中為空,則隨機新增水果

frt_tp值為1-橘子,2-蘋果

對frt_tp進行資源加鎖,更改後,釋放鎖

*/void

*fun_father

(void

* arg)

}/* 兒子執行緒:加鎖訪問frt_tp值

若值是1,則對盤子中水果減一,對盤子空間加一

重置frt_tp為0,最後釋放鎖

*/void

*fun_son

(void

* arg)

pthread_mutex_unlock

(&frt->mutex)

;sleep(1

);}return

(void*)

0;}/* 女兒執行緒:加鎖訪問frt_tp值

若值是2,則對盤子中水果減一,對盤子空間加一

重置frt_tp為0,最後釋放鎖

*/void

*fun_dau

(void

* arg)

pthread_mutex_unlock

(&frt->mutex)

;sleep(1

);}return

(void*)

0;}int

main()

執行結果如下:

father thread 0xb75bfb40 put 2 fruit, count:1

dau thread 0xb65bdb40 take 2 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 1 fruit, count:1

son thread 0xb6dbeb40 take 1 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 2 fruit, count:1

dau thread 0xb65bdb40 take 2 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 2 fruit, count:1

dau thread 0xb65bdb40 take 2 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 2 fruit, count:1

dau thread 0xb65bdb40 take 2 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 2 fruit, count:1

dau thread 0xb65bdb40 take 2 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 1 fruit, count:1

son thread 0xb6dbeb40 take 1 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 1 fruit, count:1

son thread 0xb6dbeb40 take 1 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 2 fruit, count:1

dau thread 0xb65bdb40 take 2 fruit, p_cnt:0, s_cnt:1

father thread 0xb75bfb40 put 2 fruit, count:1

dau thread 0xb65bdb40 take 2 fruit, p_cnt:0, s_cnt:1

使用到訊號量就可以達到這種效果,可以不需要使用到互斥鎖

#include

#include

#include

#include

#include

#include

typedef

struct

frt_sem_mtx_def;

/* 父親執行緒:盤子中為空,則隨機新增水果

frt_tp值為1-橘子,2-蘋果

對frt_tp進行資源加鎖,更改後,釋放鎖

*/void

*fun_father

(void

* arg)

}/* 兒子執行緒:加鎖訪問frt_tp值

若值是1,則對盤子中水果減一,對盤子空間加一

重置frt_tp為0,最後釋放鎖

*/void

*fun_son

(void

* arg)

sleep(1

);}return

(void*)

0;}/* 女兒執行緒:加鎖訪問frt_tp值

若值是2,則對盤子中水果減一,對盤子空間加一

重置frt_tp為0,最後釋放鎖

*/void

*fun_dau

(void

* arg)

sleep(1

);}return

(void*)

0;}int

main()

father thread 0xb7510b40 put 2 fruit

dau thread 0xb650eb40 take 2 fruit

father thread 0xb7510b40 put 1 fruit

son thread 0xb6d0fb40 take 1 fruit

father thread 0xb7510b40 put 2 fruit

dau thread 0xb650eb40 take 2 fruit

father thread 0xb7510b40 put 2 fruit

dau thread 0xb650eb40 take 2 fruit

father thread 0xb7510b40 put 2 fruit

dau thread 0xb650eb40 take 2 fruit

father thread 0xb7510b40 put 2 fruit

dau thread 0xb650eb40 take 2 fruit

father thread 0xb7510b40 put 1 fruit

son thread 0xb6d0fb40 take 1 fruit

father thread 0xb7510b40 put 1 fruit

son thread 0xb6d0fb40 take 1 fruit

father thread 0xb7510b40 put 2 fruit

dau thread 0xb650eb40 take 2 fruit

father thread 0xb7510b40 put 2 fruit

dau thread 0xb650eb40 take 2 fruit

father thread 0xb7510b40 put 1 fruit

son thread 0xb6d0fb40 take 1 fruit

多執行緒 訊號量

訊號量 semaphore類 建立帶指定許可數的訊號量 semaphore semaphore new semaphore 1 建立乙個許可的訊號量 訊號量用來限制訪問共享數資源的執行緒數。在訪問資源之前,執行緒必須從訊號量獲取許可,在訪問完資源後釋放訊號量。任務通過呼叫訊號量的acquire 方法...

Linux多執行緒同步 訊號量

同步主線程與子執行緒 子執行緒之間的同步 使用單個訊號量 include include include include include include void ret result thread1 void ret result thread2 sem t sem void thread1fun...

Linux 多執行緒訊號量同步

p操作 v操作 include sem t sem 定義訊號量 sem init 初始化訊號量 sem wait 獲取訊號量,訊號量的數值 1 訪問共享資源 sem post 釋放乙個訊號量,及訊號量的數值 1 sem destroy 如果不再使用訊號量,則銷毀訊號量函式和posix ipc的訊號量...