home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / pthd-0.000 / pthd-0 / pthd-0.7 / src_thread / thread.c.2 < prev    next >
Encoding:
Text File  |  1995-08-16  |  1.3 KB  |  71 lines

  1. /*
  2.  * --  thread.c.2
  3.  *     Test of pthread_delay_np() and friends
  4.  */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <pthread.h>
  8. #include "utils.h"
  9.  
  10. #define ITERATIONS (100)
  11.  
  12. static void 
  13. proc( struct timespec *tv )
  14. {
  15.    long sum = 0;
  16.    int i, st;
  17.  
  18.    for(i = 0; i < ITERATIONS; i++ )
  19.        sum += 1;
  20.    
  21.    st = pthread_delay_np( tv );
  22.    CHECK(st, "pthread_delay_np()");
  23.  
  24.    for(i = 0; i < ITERATIONS; i++ )
  25.        sum += 1;
  26.  
  27.    pthread_exit( (void *) SUCCESS );
  28. }
  29.  
  30. static pthread_t  *th;
  31.  
  32. int 
  33. main( int argc, char *argv[] )
  34. {
  35.    int i, st, exit_status, num_threads = 1;
  36.    struct timespec tv;
  37.  
  38.    if( argc == 2 )
  39.        num_threads = atoi( argv[1] );
  40.  
  41.    th = malloc_r( num_threads * sizeof( pthread_t ));
  42.  
  43.    tv.tv_sec = 10;
  44.    tv.tv_nsec = rand() % 500000 + 100000;
  45.  
  46.    for(i = 0; i < num_threads; i++ )
  47.    {
  48.        st = create_joinable( &th[i], (thread_proc_t) proc, &tv );
  49.        CHECK(st, "create_joinable()");
  50.    }
  51.  
  52.    for(i = 0; i < num_threads; i++ )
  53.    {
  54.        st = pthread_join( th[i], (void **) &exit_status );
  55.        CHECK(st, "pthread_join()");
  56.  
  57.        pthread_lock_global_np();
  58.        if( exit_status != SUCCESS )
  59.            fprintf(stderr, "Failed to join with thread[%d]!\n", i + 2 );
  60.        else
  61.            printf("Joined with thread[%d]!\n", i + 2 );
  62.        pthread_unlock_global_np();
  63.    }
  64.  
  65.    free_r( th );
  66.  
  67.    print_system_counters();
  68.    return( EXIT_SUCCESS );
  69. }
  70.  
  71.