Back to index

glibc  2.9
ex12.c
Go to the documentation of this file.
00001 /* Variant of ex6, but this time we use pthread_exit ().  */
00002 #include <errno.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <pthread.h>
00006 #include <unistd.h>
00007 
00008 static void *
00009 __attribute__ ((noreturn))
00010 test_thread (void *v_param)
00011 {
00012   pthread_exit (NULL);
00013 }
00014 
00015 int
00016 main (void)
00017 {
00018   unsigned long count;
00019 
00020   setvbuf (stdout, NULL, _IONBF, 0);
00021 
00022   for (count = 0; count < 2000; ++count)
00023     {
00024       pthread_t thread;
00025       int status;
00026 
00027       status = pthread_create (&thread, NULL, test_thread, NULL);
00028       if (status != 0)
00029        {
00030          printf ("status = %d, count = %lu: %s\n", status, count,
00031                 strerror (errno));
00032          return 1;
00033        }
00034       else
00035        {
00036          printf ("count = %lu\n", count);
00037        }
00038       /* pthread_detach (thread); */
00039       if (pthread_join (thread, NULL) != 0)
00040        {
00041          printf ("join failed, count %lu\n", count);
00042          return 2;
00043        }
00044       usleep (10);
00045     }
00046   return 0;
00047 }