Back to index

glibc  2.9
ex9.c
Go to the documentation of this file.
00001 /* Tests for pthread_barrier_* functions.
00002    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
00003    Contributed by Kaz Kylheku <kaz@ashi.footprints.net>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License as
00007    published by the Free Software Foundation; either version 2.1 of the
00008    License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 #include <errno.h>
00021 #include <error.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <pthread.h>
00025 
00026 #define NUM_THREADS 10
00027 #define NUM_ITERS   500
00028 
00029 static void *thread (void *)  __attribute__ ((__noreturn__));
00030 static pthread_barrier_t barrier;
00031 
00032 int
00033 main (void)
00034 {
00035   pthread_t thread_list[NUM_THREADS];
00036   int i;
00037 
00038   if (pthread_barrier_init (&barrier, NULL, NUM_THREADS + 1) != 0)
00039     error (EXIT_FAILURE, 0, "cannot initialize barrier");
00040 
00041   for (i = 0; i < NUM_THREADS; i++)
00042     {
00043       if (pthread_create (&thread_list[i], NULL, thread, NULL) != 0)
00044        error (EXIT_FAILURE, 0, "cannot create thread");
00045     }
00046 
00047   (void) thread (NULL);
00048 
00049   for (i = 0; i < NUM_THREADS; i++)
00050     {
00051       pthread_join(thread_list[i], NULL);
00052     }
00053 
00054   return 0;
00055 }
00056 
00057 
00058 static void *
00059 thread (void *arg)
00060 {
00061   int i;
00062   pthread_t self = pthread_self ();
00063   static pthread_t last_serial_thread;
00064   static int linecount; /* protected by flockfile(stdout) */
00065 
00066   for (i = 0; i < NUM_ITERS; i++)
00067     {
00068       switch (pthread_barrier_wait (&barrier))
00069        {
00070        case 0:
00071          flockfile (stdout);
00072          printf ("%04d: non-serial thread %lu\n", ++linecount,
00073                 (unsigned long) self);
00074          funlockfile (stdout);
00075          break;
00076        case PTHREAD_BARRIER_SERIAL_THREAD:
00077          flockfile (stdout);
00078          printf ("%04d: serial thread %lu\n", ++linecount,
00079                 (unsigned long) self);
00080          funlockfile (stdout);
00081          last_serial_thread = self;
00082          break;
00083        default:
00084          /* Huh? */
00085          error (EXIT_FAILURE, 0, "unexpected return value from barrier wait");
00086        }
00087     }
00088 
00089   if (pthread_equal (self, last_serial_thread))
00090   {
00091     flockfile (stdout);
00092     printf ("%04d: last serial thread %lu terminating process\n",
00093            ++linecount, (unsigned long) self);
00094     funlockfile (stdout);
00095   }
00096 
00097   pthread_exit(NULL);
00098 }