Back to index

glibc  2.9
ex7.c
Go to the documentation of this file.
00001 /* This is a test of the special shutdown that occurs
00002    when all threads, including the main one, call
00003    pthread_exit(). It demonstrates that atexit
00004    handlers are properly called, and that the
00005    output is properly flushed even when stdout is
00006    redirected to a file, and therefore fully buffered. */
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <pthread.h>
00011 
00012 #define NTHREADS 20         /* number of threads */
00013 
00014 static void *
00015 thread (void *arg)
00016 {
00017   printf ("thread terminating\n");
00018   return 0;
00019 }
00020 
00021 static void
00022 cleanup (void)
00023 {
00024   printf ("atexit handler called\n");
00025 }
00026 
00027 int
00028 main (void)
00029 {
00030   int i;
00031 
00032   atexit (cleanup);
00033 
00034   for (i = 0; i < NTHREADS; i++)
00035     {
00036       pthread_t id;
00037       if (pthread_create (&id, 0, thread, 0) != 0)
00038        {
00039          fprintf (stderr, "pthread_create failed\n");
00040          abort ();
00041        }
00042     }
00043 
00044   pthread_exit (0);
00045 }