-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdeadlock.cpp
More file actions
79 lines (66 loc) · 1.56 KB
/
deadlock.cpp
File metadata and controls
79 lines (66 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
查看多个线程堆栈:thread apply all bt
跳转到线程中:t 线程号
查看具体的调用堆栈:f 堆栈号
直接从pid号用gdb调试:gdb attach pid
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#define NUMBER 2
pthread_mutex_t mutex1;//定义互斥锁
pthread_mutex_t mutex2;
void *ThreadWork1(void *arg)
{
int *p = (int*)arg;
pthread_mutex_lock(&mutex1);
sleep(2);
pthread_mutex_lock(&mutex2);
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex1);
return NULL;
}
void *ThreadWork2(void *arg)
{
int *p = (int*)arg;
pthread_mutex_lock(&mutex2);
sleep(2);
pthread_mutex_lock(&mutex1);
pthread_mutex_unlock(&mutex1);
pthread_mutex_unlock(&mutex2);
return NULL;
}
int main()
{
pthread_t tid[NUMBER];
pthread_mutex_init(&mutex1,NULL);//互斥锁初始化
pthread_mutex_init(&mutex2,NULL);//互斥锁初始化
int i = 0;
int ret = pthread_create(&tid[0],NULL,ThreadWork1,(void*)&i);
if(ret != 0)
{
perror("pthread_create");
return -1;
}
ret = pthread_create(&tid[1],NULL,ThreadWork2,(void*)&i);
if(ret != 0)
{
perror("pthread_create");
return -1;
}
//pthread_join(tid,NULL);//线程等待
//pthread_join(tid,NULL);//线程等待
//pthread_detach(tid);//线程分离
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
pthread_mutex_destroy(&mutex1);//销毁互斥锁
pthread_mutex_destroy(&mutex2);//销毁互斥锁
while(1)
{
printf("i am main work thread\n");
sleep(1);
}
return 0;
}