-
Notifications
You must be signed in to change notification settings - Fork 0
/
dead_mutex.cpp
41 lines (36 loc) · 873 Bytes
/
dead_mutex.cpp
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
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
int a= 0;
int b= 0;
pthread_mutex_t mutex_a;
pthread_mutex_t mutex_b;
void* another(void* arg)
{
pthread_mutex_lock(&mutex_b);
printf("in child thread,got mutex b,waiting for mutex a\n");
sleep(5);
++b;
pthread_mutex_lock(&mutex_a);
b += a++;
pthread_mutex_unlock(&mutex_a);
pthread_mutex_unlock(&mutex_b);
pthread_exit(NULL);
}
int main()
{
pthread_t id;
pthread_mutex_init(&mutex_a,NULL);
pthread_mutex_init(&mutex_b,NULL);
pthread_create(&id,NULL,another,NULL);
pthread_mutex_lock(&mutex_a);
printf("in parent thread,got mutex a,waiting for mutex b\n");
sleep(5);
++a;
pthread_mutex_lock(&mutex_b);
a+= b++;
pthread_join(id,NULL);
pthread_mutex_destroy(&mutex_a);
pthread_mutex_destroy(&mutex_b);
return 0;
}