標籤:first creat write sign main tracking tle 結束 split
各位看官們。大家好,上一回中咱們說的是線程死結的範例,這一回咱們繼續說該範例。閑話休提,言歸正轉。讓我們一起talk C栗子吧!
看官們,由於篇幅的原因我們在上一回僅僅介紹了死結發生的第一種原因,今天我們將介紹死結發生的另外一種原因,而且該原因中的虛擬碼轉換為實際的C語言代碼。
為了方便,我們使用前面章回中示範相互排斥量的代碼,在該代碼的基礎上做一些小改動來示範死結。代碼例如以下:
首先定義兩個相互排斥量,相互排斥量是全域變數。方便線程使用。
#if MUTEX_ENABLEpthread_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 functionvoid *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 functionvoid *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語言執行個體--線程死結三)