Linux Audio

Check our new training course

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