




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第C語言多線程開發(fā)中死鎖與讀寫鎖問題詳解目錄死鎖讀寫鎖
死鎖
有時(shí),一個(gè)線程需要同時(shí)訪問兩個(gè)或更多不同的共享資源,而每個(gè)資源又都由不同的互斥量管理。當(dāng)超過一個(gè)線程加鎖同一組互斥量時(shí),就有可能發(fā)生死鎖;
兩個(gè)或兩個(gè)以上的進(jìn)程在執(zhí)行過程中,因爭(zhēng)奪共享資源而造成的一種互相等待的現(xiàn)象,若無外力作用,它們都將無法推進(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é)果:
讀寫鎖
/*
讀寫鎖的類型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í)寫這個(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è)寫線程,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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 物業(yè)客服主管崗位目標(biāo)職責(zé)他
- 部編版小學(xué)二年級(jí)語文上冊(cè)活動(dòng)課安排計(jì)劃
- 酒店員工招聘籌備計(jì)劃
- 婚慶策劃VIP包廂服務(wù)流程
- 幼兒園辦公室會(huì)議協(xié)調(diào)工作計(jì)劃
- 醫(yī)院工會(huì)委員會(huì)職責(zé)及委員職責(zé)
- 人教版六年級(jí)語文課堂復(fù)習(xí)計(jì)劃
- 交通運(yùn)輸信息化領(lǐng)導(dǎo)小組成員職責(zé)
- 幼兒園日常管理流程他
- 校服環(huán)境影響自查報(bào)告范文
- 湖北省2024年本科普通批錄取院校(首選物理)平行志愿投檔線
- 大學(xué)生全國創(chuàng)業(yè)服務(wù)網(wǎng)項(xiàng)目
- 陜西省漢中市2023-2024學(xué)年高二下學(xué)期7月期末考試 英語 含答案
- 跌倒的應(yīng)急處理課件
- 基于結(jié)構(gòu)化主題的單元整體教學(xué)-小學(xué)數(shù)學(xué)實(shí)踐探索
- 人工智能技術(shù)在人力資源管理中的應(yīng)用研究
- 2024年1月遼寧高中學(xué)業(yè)水平合格考數(shù)學(xué)試卷真題(含答案詳解)
- 私人房屋抵押合同
- 腹瀉課件模板
- 《市場(chǎng)人員商務(wù)禮儀》課件
- 《OSB-單板復(fù)合集裝箱底板剛度模型及工藝研究》
評(píng)論
0/150
提交評(píng)論