(res_isourserver): Fix cast.
[glibc/pb-stable.git] / linuxthreads / Examples / ex9.c
blob0c087410035d4272b1055645a363ed0e4a984ffc
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. */
20 #include <errno.h>
21 #include <error.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <pthread.h>
26 #define NUM_THREADS 10
27 #define NUM_ITERS 500
29 static void *thread (void *);
30 static pthread_barrier_t barrier;
32 int
33 main (void)
35 pthread_t th;
36 int i;
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");
47 (void) thread (NULL);
48 /* notreached */
49 return 0;
53 static void *
54 thread (void *arg)
56 int i;
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))
65 case 0:
66 flockfile (stdout);
67 printf ("%04d: non-serial thread %lu\n", ++linecount,
68 (unsigned long) self);
69 funlockfile (stdout);
70 break;
71 case PTHREAD_BARRIER_SERIAL_THREAD:
72 flockfile (stdout);
73 printf ("%04d: serial thread %lu\n", ++linecount,
74 (unsigned long) self);
75 funlockfile (stdout);
76 last_serial_thread = self;
77 break;
78 default:
79 /* Huh? */
80 error (EXIT_FAILURE, 0, "unexpected return value from barrier wait");
84 if (pthread_equal (self, last_serial_thread))
86 flockfile (stdout);
87 printf ("%04d: last serial thread %lu terminating process\n",
88 ++linecount, (unsigned long) self);
89 funlockfile (stdout);
90 exit (0);
93 pthread_exit(NULL);