Back to index

glibc  2.9
ex1.c
Go to the documentation of this file.
00001 /* Creates two threads, one printing 10000 "a"s, the other printing
00002    10000 "b"s.
00003    Illustrates: thread creation, thread joining. */
00004 
00005 #include <stddef.h>
00006 #include <stdio.h>
00007 #include <unistd.h>
00008 #include "pthread.h"
00009 
00010 static void *
00011 process (void *arg)
00012 {
00013   int i;
00014   fprintf (stderr, "Starting process %s\n", (char *) arg);
00015   for (i = 0; i < 10000; i++)
00016     {
00017       write (1, (char *) arg, 1);
00018     }
00019   return NULL;
00020 }
00021 
00022 int
00023 main (void)
00024 {
00025   int retcode;
00026   pthread_t th_a, th_b;
00027   void *retval;
00028 
00029   retcode = pthread_create (&th_a, NULL, process, (void *) "a");
00030   if (retcode != 0)
00031     fprintf (stderr, "create a failed %d\n", retcode);
00032   retcode = pthread_create (&th_b, NULL, process, (void *) "b");
00033   if (retcode != 0)
00034     fprintf (stderr, "create b failed %d\n", retcode);
00035   retcode = pthread_join (th_a, &retval);
00036   if (retcode != 0)
00037     fprintf (stderr, "join a failed %d\n", retcode);
00038   retcode = pthread_join (th_b, &retval);
00039   if (retcode != 0)
00040     fprintf (stderr, "join b failed %d\n", retcode);
00041   return 0;
00042 }