-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gate_Lock_example.c
52 lines (40 loc) · 935 Bytes
/
Gate_Lock_example.c
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t y = PTHREAD_MUTEX_INITIALIZER;;
pthread_mutex_t z = PTHREAD_MUTEX_INITIALIZER;
void *
threading1(void * ptr)
{
pthread_mutex_lock(&x);
pthread_mutex_lock(&y);
pthread_mutex_lock(&z);
pthread_mutex_unlock(&z);
pthread_mutex_unlock(&y);
pthread_mutex_unlock(&x);
return 0x0 ;
}
void *
threading2(void * ptr)
{
pthread_mutex_lock(&x);
pthread_mutex_lock(&z);
pthread_mutex_lock(&y);
pthread_mutex_unlock(&y);
pthread_mutex_unlock(&z);
pthread_mutex_unlock(&x);
return 0x0 ;
}
int
main()
{
pthread_t thread1 ;
pthread_t thread2 ;
pthread_create(&(thread1), 0x0 ,threading1 , 0x0) ;
pthread_create(&(thread2), 0x0 , threading2, 0x0) ;
pthread_join(thread1, 0x0) ;
pthread_join(thread2, 0x0) ;
exit(0) ;
}