死锁示例代码
死鎖產生實例,兩個線程兩個互斥鎖,每個線程占有一個互斥鎖,同時想獲得另一個互斥鎖則會產生死鎖。
解決方案:
1.一次占有全部資源
2.每個線程占有鎖的順序是一致的。比如都是同時占有A,然后占有B鎖。
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <pthread.h> #include <signal.h>pthread_mutex_t mutex_one,mutex_two; pthread_mutex_t mutex; pthread_cond_t cond;void *thread_routine_one(void *arg) { pthread_cond_wait(&cond,&mutex);//確保two線程先運行 printf("thread_routine_one: lock mutex_one!\n"); pthread_mutex_lock(&mutex_one);printf("thread_routine_one: lock mutex_two!\n"); pthread_mutex_lock(&mutex_two);//獲取two鎖,這個要等待two線程對two鎖的釋放 sleep(1); printf("thread_routine_one: unlock mutex_two!\n"); pthread_mutex_unlock(&mutex_two);printf("thread_routine_one: unlock mutex_one!\n"); pthread_mutex_unlock(&mutex_one);return NULL;}void *thread_routine_two(void *arg) { printf("thread_routine_two: lock mutex_two!\n"); pthread_mutex_lock(&mutex_two);//獲取two鎖 pthread_cond_signal(&cond);//讓one線程運行 sleep(1);//休眠,讓one可以先運行獲取one鎖 printf("thread_routine_two: lock mutex_one!\n");pthread_mutex_lock(&mutex_one);//獲取one鎖,這個同樣必須等待one線程多one鎖的釋放/**這時出現死鎖現象了,在two線程要等待one線程對one鎖的釋放,同時two鎖沒有釋放 * * 然而在one線程需要等待two線程對two鎖的釋放,然后才會對one鎖的釋放!出現相互等待的過程**/printf("thread_routine_two: unlock mutex_one!\n"); pthread_mutex_unlock(&mutex_one);printf("thread_routine_two: unlock mutex_two!\n"); pthread_mutex_unlock(&mutex_two);return NULL;}void main() { pthread_t pthread_one,pthread_two;pthread_mutex_init(&mutex_one,NULL); pthread_mutex_init(&mutex_two,NULL); pthread_mutex_init(&mutex,NULL); pthread_cond_init(&cond,NULL);pthread_create(&pthread_one,NULL,thread_routine_one,NULL); pthread_create(&pthread_two,NULL,thread_routine_two,NULL);while(1) sleep(1);}
解決方案:
1.一次占有全部資源
2.每個線程占有鎖的順序是一致的。比如都是同時占有A,然后占有B鎖。
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <pthread.h> #include <signal.h>pthread_mutex_t mutex_one,mutex_two; pthread_mutex_t mutex; pthread_cond_t cond;void *thread_routine_one(void *arg) { pthread_cond_wait(&cond,&mutex);//確保two線程先運行 printf("thread_routine_one: lock mutex_one!\n"); pthread_mutex_lock(&mutex_one);printf("thread_routine_one: lock mutex_two!\n"); pthread_mutex_lock(&mutex_two);//獲取two鎖,這個要等待two線程對two鎖的釋放 sleep(1); printf("thread_routine_one: unlock mutex_two!\n"); pthread_mutex_unlock(&mutex_two);printf("thread_routine_one: unlock mutex_one!\n"); pthread_mutex_unlock(&mutex_one);return NULL;}void *thread_routine_two(void *arg) { printf("thread_routine_two: lock mutex_two!\n"); pthread_mutex_lock(&mutex_two);//獲取two鎖 pthread_cond_signal(&cond);//讓one線程運行 sleep(1);//休眠,讓one可以先運行獲取one鎖 printf("thread_routine_two: lock mutex_one!\n");pthread_mutex_lock(&mutex_one);//獲取one鎖,這個同樣必須等待one線程多one鎖的釋放/**這時出現死鎖現象了,在two線程要等待one線程對one鎖的釋放,同時two鎖沒有釋放 * * 然而在one線程需要等待two線程對two鎖的釋放,然后才會對one鎖的釋放!出現相互等待的過程**/printf("thread_routine_two: unlock mutex_one!\n"); pthread_mutex_unlock(&mutex_one);printf("thread_routine_two: unlock mutex_two!\n"); pthread_mutex_unlock(&mutex_two);return NULL;}void main() { pthread_t pthread_one,pthread_two;pthread_mutex_init(&mutex_one,NULL); pthread_mutex_init(&mutex_two,NULL); pthread_mutex_init(&mutex,NULL); pthread_cond_init(&cond,NULL);pthread_create(&pthread_one,NULL,thread_routine_one,NULL); pthread_create(&pthread_two,NULL,thread_routine_two,NULL);while(1) sleep(1);}
?
轉載于:https://www.cnblogs.com/shenshenlei/p/5983869.html
總結
- 上一篇: hibernate级联操作详解
- 下一篇: vim学习日志(5):vim下wimrc