1 /* $NetBSD: barrier1.c,v 1.2 2006/12/23 17:44:12 ad Exp $ */
10 void *threadfunc(void *arg
);
12 pthread_barrier_t barrier
;
13 pthread_mutex_t mutex
;
15 int after_barrier_count
;
20 main(int argc
, char *argv
[])
26 pthread_mutex_init(&mutex
, NULL
);
28 ret
= pthread_barrier_init(&barrier
, NULL
, COUNT
);
30 err(1, "pthread_barrier_init");
32 for (i
= 0; i
< COUNT
; i
++) {
33 pthread_mutex_lock(&mutex
);
34 assert(after_barrier_count
== 0);
35 pthread_mutex_unlock(&mutex
);
36 ret
= pthread_create(&new[i
], NULL
, threadfunc
,
39 err(1, "pthread_create");
43 for (i
= 0; i
< COUNT
; i
++) {
44 ret
= pthread_join(new[i
], &joinval
);
46 err(1, "pthread_join: %d", ret
);
47 pthread_mutex_lock(&mutex
);
48 assert(after_barrier_count
> i
);
49 pthread_mutex_unlock(&mutex
);
50 printf("main joined with thread %d\n", i
);
53 pthread_mutex_lock(&mutex
);
54 assert(after_barrier_count
== COUNT
);
55 pthread_mutex_unlock(&mutex
);
56 assert(serial_count
== 1);
64 int which
= (int)(long)arg
;
67 printf("thread %d entering barrier\n", which
);
68 ret
= pthread_barrier_wait(&barrier
);
69 printf("thread %d leaving barrier -> %d\n", which
, ret
);
71 pthread_mutex_lock(&mutex
);
72 after_barrier_count
++;
73 if (ret
== PTHREAD_BARRIER_SERIAL_THREAD
)
75 pthread_mutex_unlock(&mutex
);