4 pthread_mutex_t a
= PTHREAD_MUTEX_INITIALIZER
;
5 pthread_mutex_t b
= PTHREAD_MUTEX_INITIALIZER
;
8 void *ba_lock(void *arg
)
12 pthread_mutex_lock(&b
);
14 if (pthread_barrier_wait(&bar
) == PTHREAD_BARRIER_SERIAL_THREAD
)
15 pthread_barrier_destroy(&bar
);
17 pthread_mutex_lock(&a
);
19 pthread_mutex_unlock(&a
);
20 pthread_mutex_unlock(&b
);
27 pthread_barrier_init(&bar
, NULL
, 2);
29 if (pthread_create(&t
, NULL
, ba_lock
, NULL
)) {
30 fprintf(stderr
, "pthread_create() failed\n");
33 pthread_mutex_lock(&a
);
35 if (pthread_barrier_wait(&bar
) == PTHREAD_BARRIER_SERIAL_THREAD
)
36 pthread_barrier_destroy(&bar
);
38 pthread_mutex_lock(&b
);
40 pthread_mutex_unlock(&b
);
41 pthread_mutex_unlock(&a
);
43 pthread_join(t
, NULL
);