自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

兩個(gè)線程,兩個(gè)互斥鎖,怎么形成一個(gè)死循環(huán)?

網(wǎng)絡(luò) 通信技術(shù)
為了保證主線程和子線程都能夠分別獲得鎖mutex1、mutex2,各自獲得鎖后一定要先sleep 1秒鐘,否則創(chuàng)建完子線程后,主線程還有一定的時(shí)間片,主線程會申請到鎖mutex2,無法形成死鎖。

[[351971]]

粉絲的提問,必須安排。

兩個(gè)線程,兩個(gè)互斥鎖如何形成死鎖?

程序流程圖如下:

程序流程圖

如上圖所示:

  1. t0時(shí)刻,主線程創(chuàng)建子線程,并初始化互斥鎖mutex1、mutex2;
  2. t1時(shí)刻,主線程申請到了mutex1、子線程申請到了mutex2;
  3. t2時(shí)刻,主線程和子線程都sleep 1秒鐘,防止優(yōu)先獲得時(shí)間片的線程直接申請到了另外1個(gè)互斥鎖,導(dǎo)致程序直接退出;
  4. t3時(shí)刻,主線程和子線程都想獲得對方手里的互斥鎖,但是對方都來不及釋放自己手里的鎖;
  5. t4時(shí)刻,主線程和子線雙雙進(jìn)入休眠。

【注意】為了保證主線程和子線程都能夠分別獲得鎖mutex1、mutex2,各自獲得鎖后一定要先sleep 1秒鐘,否則創(chuàng)建完子線程后,主線程還有一定的時(shí)間片,主線程會申請到鎖mutex2,無法形成死鎖。

死鎖

源碼如下

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <string.h> 
  4. #include <pthread.h>    
  5.  
  6. unsigned int value1, value2, count
  7. pthread_mutex_t  mutex1,mutex2; 
  8. void *function(void *arg); 
  9.  
  10.  
  11. void  *function(void *arg) 
  12.  pthread_mutex_lock(&mutex2); 
  13.  printf("new thread get mutex2\n");  
  14.  sleep(1); 
  15.  pthread_mutex_lock(&mutex1);  
  16.  printf("new thread get mutex1\n");  
  17.   
  18.   
  19.  pthread_mutex_unlock(&mutex1); 
  20.  printf("new thread release mutex1\n"); 
  21.  pthread_mutex_unlock(&mutex2);  
  22.  printf("new thread release mutex2\n"); 
  23.     return  NULL
  24.  }   
  25.  
  26. int main(int argc,  char *argv[]) 
  27.  pthread_t  a_thread; 
  28.           
  29.  if (pthread_mutex_init(&mutex1, NULL) < 0) 
  30.  { 
  31.   perror("fail to mutex_init"); 
  32.   exit(-1); 
  33.  } 
  34.   if (pthread_mutex_init(&mutex2, NULL) < 0) 
  35.  { 
  36.   perror("fail to mutex_init"); 
  37.   exit(-1); 
  38.  }               
  39.  if (pthread_create(&a_thread, NULLfunctionNULL) < 0) 
  40.  {    
  41.   perror("fail to pthread_create");      
  42.   exit(-1); 
  43.  } 
  44.     while ( 1 ) 
  45.     { 
  46.         pthread_mutex_lock(&mutex1); 
  47.   printf("main thread get mutex1\n"); 
  48.   sleep(1); 
  49.         pthread_mutex_lock(&mutex2);   
  50.   printf("main thread get mutex2\n"); 
  51.         pthread_mutex_unlock(&mutex2); 
  52.   printf("main thread release mutex2\n"); 
  53.         pthread_mutex_unlock(&mutex1); 
  54.   printf("main thread release mutex1\n"); 
  55.     } 
  56.     return 0; 
  57.  }              
  58. 編譯運(yùn)行 

編譯運(yùn)行

從執(zhí)行結(jié)果可以判斷,主線程和子線程分別獲得了互斥鎖mutex1、mutex2,sleep 1秒后,他們都想再分別申請mutex2、mutex1,而雙方都不想釋放自己手中的鎖,鎖已形成了死鎖,程序就一直處于休眠狀態(tài)。

查看下該進(jìn)程的線程

查看進(jìn)程ID,為4204

查看該進(jìn)程創(chuàng)建的線程id:4204、4205。

本文轉(zhuǎn)載自微信公眾號「一口Linux」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系一口Linux公眾號。

 

責(zé)任編輯:武曉燕 來源: 一口Linux
相關(guān)推薦

2022-07-29 12:56:19

寬帶路由器設(shè)置

2022-06-17 09:46:51

Chrome 102Chrome瀏覽器

2013-10-11 09:32:33

TD-LTELTE FDD4G

2020-10-26 08:19:53

算法隊(duì)列

2009-07-16 10:39:00

SwingUtilit

2010-09-10 15:26:05

SOAP封裝

2021-07-29 06:56:35

前端事件循環(huán)

2021-12-02 06:58:02

項(xiàng)目事件循環(huán)

2021-11-15 09:53:16

STM32PSPMSP

2019-06-25 10:46:04

Flutter開發(fā)APP

2021-08-03 08:13:47

數(shù)據(jù)

2009-07-15 18:29:22

Jython應(yīng)用

2017-01-15 01:45:37

簡歷簡歷模板數(shù)據(jù)

2010-07-02 12:26:51

LEACH協(xié)議

2010-09-17 09:51:37

SIP路由

2009-06-30 10:37:59

Linux操作系統(tǒng)

2009-05-19 16:04:04

甲骨文Sun

2015-05-06 10:28:32

移動應(yīng)用谷歌

2020-11-10 07:13:44

端口號進(jìn)程

2021-02-20 21:04:53

人工智能機(jī)器學(xué)習(xí)技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號