Commit | Line | Data |
---|---|---|
11a1ac20 AAF |
1 | #include <stdio.h> |
2 | #include <pthread.h> | |
3 | ||
4 | pthread_mutex_t a = PTHREAD_MUTEX_INITIALIZER; | |
5 | pthread_mutex_t b = PTHREAD_MUTEX_INITIALIZER; | |
6 | pthread_barrier_t bar; | |
7 | ||
8 | void *ba_lock(void *arg) | |
9 | { | |
10 | int ret, i; | |
11 | ||
12 | pthread_mutex_lock(&b); | |
13 | ||
14 | if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD) | |
15 | pthread_barrier_destroy(&bar); | |
16 | ||
17 | pthread_mutex_lock(&a); | |
18 | ||
19 | pthread_mutex_unlock(&a); | |
20 | pthread_mutex_unlock(&b); | |
21 | } | |
22 | ||
23 | int main(void) | |
24 | { | |
25 | pthread_t t; | |
26 | ||
27 | pthread_barrier_init(&bar, NULL, 2); | |
28 | ||
29 | if (pthread_create(&t, NULL, ba_lock, NULL)) { | |
30 | fprintf(stderr, "pthread_create() failed\n"); | |
31 | return 1; | |
32 | } | |
33 | pthread_mutex_lock(&a); | |
34 | ||
35 | if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD) | |
36 | pthread_barrier_destroy(&bar); | |
37 | ||
38 | pthread_mutex_lock(&b); | |
39 | ||
40 | pthread_mutex_unlock(&b); | |
41 | pthread_mutex_unlock(&a); | |
42 | ||
43 | pthread_join(t, NULL); | |
44 | ||
45 | return 0; | |
46 | } |