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