Home | History | Annotate | Line # | Download | only in c
      1 /* Compiler options:
      2 #progos: linux
      3 #cc: additional_flags=-pthread
      4 #output: Starting process a\naaaaaaaaStarting process b\nababbbbbbbbb
      5 
      6    The output will change depending on the exact syscall sequence per
      7    thread, so will change with glibc versions.  Prepare to modify; use
      8    the latest glibc.
      9 
     10    This file is from glibc/linuxthreads, with the difference that the
     11    number is 10, not 10000.  */
     12 
     13 /* Creates two threads, one printing 10000 "a"s, the other printing
     14    10000 "b"s.
     15    Illustrates: thread creation, thread joining. */
     16 
     17 #include <stddef.h>
     18 #include <stdio.h>
     19 #include <unistd.h>
     20 #include "pthread.h"
     21 
     22 static void *
     23 process (void *arg)
     24 {
     25   int i;
     26   fprintf (stderr, "Starting process %s\n", (char *) arg);
     27   for (i = 0; i < 10; i++)
     28     {
     29       write (1, (char *) arg, 1);
     30     }
     31   return NULL;
     32 }
     33 
     34 int
     35 main (void)
     36 {
     37   int retcode;
     38   pthread_t th_a, th_b;
     39   void *retval;
     40 
     41   retcode = pthread_create (&th_a, NULL, process, (void *) "a");
     42   if (retcode != 0)
     43     fprintf (stderr, "create a failed %d\n", retcode);
     44   retcode = pthread_create (&th_b, NULL, process, (void *) "b");
     45   if (retcode != 0)
     46     fprintf (stderr, "create b failed %d\n", retcode);
     47   retcode = pthread_join (th_a, &retval);
     48   if (retcode != 0)
     49     fprintf (stderr, "join a failed %d\n", retcode);
     50   retcode = pthread_join (th_b, &retval);
     51   if (retcode != 0)
     52     fprintf (stderr, "join b failed %d\n", retcode);
     53   return 0;
     54 }
     55