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