一起talk C栗子吧(第一百一十九回:C語言實例--線程死鎖三)
阿新 • • 發佈:2017-06-26
取數 string value reading sel 一個 second key 而且
各位看官們。大家好,上一回中咱們說的是線程死鎖的樣例,這一回咱們繼續說該樣例。閑話休提,言歸正轉。讓我們一起talk C栗子吧!
看官們,由於篇幅的原因我們在上一回僅僅介紹了死鎖發生的第一種原因,今天我們將介紹死鎖發生的另外一種原因,而且該原因中的偽代碼轉換為實際的C語言代碼。
為了方便,我們使用前面章回中演示相互排斥量的代碼,在該代碼的基礎上做一些小改動來演示死鎖。代碼例如以下:
首先定義兩個相互排斥量,相互排斥量是全局變量。方便線程使用。
#if MUTEX_ENABLE
pthread_mutex_t mutex_value1;
pthread_mutex_t mutex_value2;
#endif
接下來在主進程中(也就是main函數)初始化兩個相互排斥量:
res = pthread_mutex_init(&mutex_value1,NULL);
res = pthread_mutex_init(&mutex_value2,NULL);
在主進程的最後還要記得釋放與相互排斥量相關的資源:
#if MUTEX_ENABLE
//destroy mutex
res = pthread_mutex_destroy(&mutex_value1);
res = pthread_mutex_destroy(&mutex_value2);
#endif
我們分別改動兩個線程的執行函數,該段代碼是核心代碼,請大家細致閱讀:
// the first thread function
void *thread_func1(void *param)
{
int i = 0;
int res = 0;
pthread_t thread_id;
thread_id = pthread_self();
printf("Thread ID::%u -----------S---------- \n",(unsigned int)thread_id);
while(i++ < 4)
{
#if MUTEX_ENABLE
res = pthread_mutex_lock(&mutex_value1); // mutex1 is locking
if(res != 0)
{
printf(" mutex1 lock failed \n");
}
#endif
read_data("Thread_1");
#if MUTEX_ENABLE
res = pthread_mutex_lock(&mutex_value2); //mutex2 is locking
if(res != 0)
{
printf(" mutex2 lock failed \n");
}
#endif
#if MUTEX_ENABLE
res = pthread_mutex_unlock(&mutex_value2);
if(res != 0)
{
printf(" mutex2 unlock failed \n");
}
res = pthread_mutex_unlock(&mutex_value1);
if(res != 0)
{
printf(" mutex1 unlock failed \n");
}
#endif
sleep(2);
}
printf("Thread ID::%u -----------E---------- \n",(unsigned int)thread_id);
pthread_exit(&status); // end the thread
}
// the second thread function
void *thread_func2(void *param)
{
int i = 0;
int res = 0;
pthread_t thread_id;
thread_id = pthread_self();
printf("Thread ID::%u -----------S---------- \n",(unsigned int)thread_id);
while(i++ < 4)
{
#if MUTEX_ENABLE
res = pthread_mutex_lock(&mutex_value2); //mutex 2 is locking
if(res != 0)
{
printf(" mutex2 lock failed \n");
}
#endif
write_data("Thread_2");
#if MUTEX_ENABLE
res = pthread_mutex_lock(&mutex_value1); //mutex 1 is locking
if(res != 0)
{
printf(" mutex1 lock failed \n");
}
#endif
#if MUTEX_ENABLE
res = pthread_mutex_unlock(&mutex_value1);
if(res != 0)
{
printf(" mutex1 unlock failed \n");
}
#endif
#if MUTEX_ENABLE
res = pthread_mutex_unlock(&mutex_value2);
if(res != 0)
{
printf(" mutex2 unlock failed \n");
}
#endif
sleep(1);
}
printf("Thread ID::%u -----------E---------- \n",(unsigned int)thread_id);
pthread_exit(&status); // end the thread
}
我們執行上面的程序,能夠得到下面結果:
Create first thread //創建第一個線程
Create second thread //創建第二個線程
Thread ID::3076344640 -----------S----------
[Thread_1] start reading data //第一個線程讀取數據,同一時候對相互排斥量一加鎖
Thread ID::3067951936 -----------S----------
[Thread_2] start writing data //第二個線程改動數據,同一時候對相互排斥量二加鎖
[Thread_1] data = 0
[Thread_1] end reading data //第一個線程讀取數據結束,同一時候等待相互排斥量二被解鎖
[Thread_2] data = 1
[Thread_2] end writing data //第二個線程改動數據結束。同一時候等待相互排斥量一被解鎖
mutex2 can‘t be destroyed //發生死鎖,程序執行錯誤
從上面的程序執行結果中能夠看到,線程1鎖住了相互排斥量一,同一時候等待相互排斥量二;而線程2鎖住了相互排斥量二,同一時候等待相互排斥量一。這樣便造成了死鎖,進而引起了程序執行錯誤。
該程序是為了演示死鎖的原因專門寫的,這樣敲代碼不合理。由於它不能同步線程。大家不要拘泥於程序的內容,重點是理解死鎖是怎樣發生的。
看官們,正文中就不寫代碼了,完畢的代碼放到了我的資源中,大家能夠點擊這裏下載使用。
各位看官,關於線程死鎖的樣例咱們就講到這裏。
欲知後面還有什麽樣例,且聽下回分解 。
一起talk C栗子吧(第一百一十九回:C語言實例--線程死鎖三)