1 /* Tests for pthread_barrier_* functions.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
26 #define NUM_THREADS 10
29 static void *thread (void *);
30 static pthread_barrier_t barrier
;
38 if (pthread_barrier_init (&barrier
, NULL
, NUM_THREADS
+ 1) != 0)
39 error (EXIT_FAILURE
, 0, "cannot initialize barrier");
41 for (i
= 0; i
< NUM_THREADS
; i
++)
43 if (pthread_create (&th
, NULL
, thread
, NULL
) != 0)
44 error (EXIT_FAILURE
, 0, "cannot create thread");
57 pthread_t self
= pthread_self ();
58 static pthread_t last_serial_thread
;
59 static int linecount
; /* protected by flockfile(stdout) */
61 for (i
= 0; i
< NUM_ITERS
; i
++)
63 switch (pthread_barrier_wait (&barrier
))
67 printf ("%04d: non-serial thread %lu\n", ++linecount
,
68 (unsigned long) self
);
71 case PTHREAD_BARRIER_SERIAL_THREAD
:
73 printf ("%04d: serial thread %lu\n", ++linecount
,
74 (unsigned long) self
);
76 last_serial_thread
= self
;
80 error (EXIT_FAILURE
, 0, "unexpected return value from barrier wait");
84 if (pthread_equal (self
, last_serial_thread
))
87 printf ("%04d: last serial thread %lu terminating process\n",
88 ++linecount
, (unsigned long) self
);