threads.c
changeset 0 af501b0c1716
child 8 96d16dfe787a
equal deleted inserted replaced
-1:000000000000 0:af501b0c1716
       
     1 /**
       
     2  *     $Id: threads.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
       
     3  * $Source: /development/c/demos/threads.c,v $
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 
       
     9 #include <pthread.h>
       
    10 #include <time.h>
       
    11 
       
    12 void *thread_func (void *vptr_args);
       
    13 
       
    14 void do_some_work (void)
       
    15 {
       
    16     time_t start_time = time (NULL);
       
    17 
       
    18     while (time (NULL) == start_time);  /* busy-wait for 0-1 seconds */
       
    19 }
       
    20 
       
    21 int main (int argc, char **argv)
       
    22 {
       
    23     int i;
       
    24     pthread_t thread;
       
    25 
       
    26     pthread_create (&thread, NULL, &thread_func, NULL);
       
    27 
       
    28     for (i = 0; i < 20; ++i) {
       
    29         fprintf (stdout, "a\n");
       
    30 
       
    31         do_some_work ();
       
    32     }
       
    33 
       
    34     pthread_join (thread, NULL);
       
    35 
       
    36     exit (EXIT_SUCCESS);
       
    37 }
       
    38 
       
    39 void *thread_func (void *vptr_args)
       
    40 {
       
    41     int i;
       
    42 
       
    43     for (i = 0; i < 20; ++i) {
       
    44         fprintf (stderr, "  b\n");
       
    45 
       
    46         do_some_work ();
       
    47     }
       
    48     return NULL;
       
    49 }