




下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第C語(yǔ)言多線程開(kāi)發(fā)中死鎖與讀寫(xiě)鎖問(wèn)題詳解目錄死鎖讀寫(xiě)鎖
死鎖
有時(shí),一個(gè)線程需要同時(shí)訪問(wèn)兩個(gè)或更多不同的共享資源,而每個(gè)資源又都由不同的互斥量管理。當(dāng)超過(guò)一個(gè)線程加鎖同一組互斥量時(shí),就有可能發(fā)生死鎖;
兩個(gè)或兩個(gè)以上的進(jìn)程在執(zhí)行過(guò)程中,因爭(zhēng)奪共享資源而造成的一種互相等待的現(xiàn)象,若無(wú)外力作用,它們都將無(wú)法推進(jìn)下去。此時(shí)稱系統(tǒng)處于死鎖狀態(tài)或系統(tǒng)產(chǎn)生了死鎖。
死鎖的幾種場(chǎng)景:
忘記釋放鎖重復(fù)加鎖(重復(fù)加相同的鎖)多線程多鎖,搶占鎖資源
//多線程多鎖,搶占鎖資源
#includestdio.h
#includepthread.h
#includeunistd.h
//創(chuàng)建2個(gè)互斥量
pthread_mutex_tmutex1,mutex2;
void*workA(void*arg){
pthread_mutex_lock(mutex1);
sleep(1);
pthread_mutex_lock(mutex2);
printf("workA....\n");
pthread_mutex_unlock(mutex2);
pthread_mutex_unlock(mutex1);
returnNULL;
void*workB(void*arg){
pthread_mutex_lock(mutex2);
sleep(1);
pthread_mutex_lock(mutex1);
printf("workB....\n");
pthread_mutex_unlock(mutex1);
pthread_mutex_unlock(mutex2);
returnNULL;
intmain(){
//初始化互斥量
pthread_mutex_init(mutex1,NULL);
pthread_mutex_init(mutex2,NULL);
//創(chuàng)建2個(gè)子線程
pthread_ttid1,tid2;
pthread_create(tid1,NULL,workA,NULL);
pthread_create(tid2,NULL,workB,NULL);
//回收子線程資源
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
//釋放互斥量資源
pthread_mutex_destroy(mutex1);
pthread_mutex_destroy(mutex2);
return0;
}
執(zhí)行結(jié)果:
讀寫(xiě)鎖
/*
讀寫(xiě)鎖的類型pthread_rwlock_t
intpthread_rwlock_init(pthread_rwlock_t*restrictrwlock,constpthread_rwlockattr_t*restrictattr);
intpthread_rwlock_destroy(pthread_rwlock_t*rwlock);
intpthread_rwlock_rdlock(pthread_rwlock_t*rwlock);
intpthread_rwlock_tryrdlock(pthread_rwlock_t*rwlock);
intpthread_rwlock_wrlock(pthread_rwlock_t*rwlock);
intpthread_rwlock_trywrlock(pthread_rwlock_t*rwlock);
intpthread_rwlock_unlock(pthread_rwlock_t*rwlock);
案例:8個(gè)線程操作同一個(gè)全局變量。
3個(gè)線程不定時(shí)寫(xiě)這個(gè)全局變量,5個(gè)線程不定時(shí)的讀這個(gè)全局變量
#includestdio.h
#includepthread.h
#includeunistd.h
//創(chuàng)建一個(gè)共享數(shù)據(jù)
intnum=1;
//pthread_mutex_tmutex;
pthread_rwlock_trwlock;
void*writeNum(void*arg){
while(1){
pthread_rwlock_wrlock(rwlock);
num++;
printf("++write,tid:%ld,num:%d\n",pthread_self(),num);
pthread_rwlock_unlock(rwlock);
usleep(100);
returnNULL;
void*readNum(void*arg){
while(1){
pthread_rwlock_rdlock(rwlock);
printf("===read,tid:%ld,num:%d\n",pthread_self(),num);
pthread_rwlock_unlock(rwlock);
usleep(100);
returnNULL;
intmain(){
pthread_rwlock_init(rwlock,NULL);
//創(chuàng)建3個(gè)寫(xiě)線程,5個(gè)讀線程
pthread_twtids[3],rtids[5];
for(inti=0;ii++){
pthread_create(wtids[i],NULL,writeNum,NULL);
for(inti=0;ii++){
pthread_create(rtids[i],NULL,readNum,NULL);
//設(shè)置線程分離
for(inti=0;ii++){
pthread_detach(wtids[i]);
for(inti=0;ii
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中級(jí)審計(jì)師考試策略新思維試題及答案
- 護(hù)師臨床實(shí)踐試題及答案概述
- 中級(jí)審計(jì)師考試職業(yè)前景試題及答案
- 2025年建造師考試成功備考的主要因素試題及答案
- 安全生產(chǎn)考核證書(shū)試題及答案概覽
- 2024年財(cái)務(wù)報(bào)表解讀試題及答案
- 2024年外語(yǔ)考試安全管理專業(yè)知識(shí)試題及答案
- 安全生產(chǎn)中相關(guān)利益方的溝通與協(xié)作策略試題及答案
- 企業(yè)智能審計(jì)技術(shù)試題及答案
- 初級(jí)審計(jì)氣質(zhì)特征與試題及答案綜合考量
- 萬(wàn)用表校準(zhǔn)報(bào)告
- (部編版一年級(jí)下冊(cè))語(yǔ)文第七單元復(fù)習(xí)課件
- 新聞采訪與寫(xiě)作(馬工程筆記)
- DB32∕T 1703-2011 科技成果轉(zhuǎn)化服務(wù)規(guī)范總則
- SQ-02-綠色食品種植產(chǎn)品調(diào)查表0308
- 視頻結(jié)構(gòu)化大數(shù)據(jù)平臺(tái)解決方案
- SolidWorks、CAD三維建模練習(xí)習(xí)題圖
- 麗聲北極星分級(jí)繪本第二級(jí)上Dinner for a Dragon 教學(xué)設(shè)計(jì)
- 活躍氣氛的開(kāi)場(chǎng)小游戲「培訓(xùn)破冰前必備」
- 光伏發(fā)電項(xiàng)目安全專項(xiàng)投資估算方案
- 上海證券交易所(課堂PPT)
評(píng)論
0/150
提交評(píng)論