home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / compiler / sample02 / sample02.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-20  |  7.0 KB  |  179 lines

  1. /******************************************************************************/
  2. /* SAMPLE PROGRAM 02: SAMPLE02.C                                              */
  3. /*                                                                            */
  4. /* COPYRIGHT:                                                                 */
  5. /* ----------                                                                 */
  6. /* Copyright (C) International Business Machines Corp., 1991,1995.            */
  7. /*                                                                            */
  8. /* DISCLAIMER OF WARRANTIES:                                                  */
  9. /* -------------------------                                                  */
  10. /* The following [enclosed] code is sample code created by IBM                */
  11. /* Corporation.  This sample code is not part of any standard IBM product     */
  12. /* and is provided to you solely for the purpose of assisting you in the      */
  13. /* development of your applications.  The code is provided "AS IS",           */
  14. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  15. /* arising out of your use of the sample code, even if they have been         */
  16. /* advised of the possibility of such damages.                                */
  17. /*                                                                            */
  18. /******************************************************************************/
  19. /*                                                                            */
  20. /*  Sample to demonstrate a multithread program                               */
  21. /*                                                                            */
  22. /*  This program demonstrates the multithread programming capability          */
  23. /*  of the IBM VisualAge C++ compiler.                                        */
  24. /*                                                                            */
  25. /*  This sample program creates one thread for each argument.  Each           */
  26. /*  thread prints "Hello world from thread n!" the number of times            */
  27. /*  specified in the corresponding argument.  For example                     */
  28. /*                                                                            */
  29. /*     SAMPLE02 2 4 6                                                         */
  30. /*                                                                            */
  31. /*  creates three threads; the first thread displays "Hello"                  */
  32. /*  two times, the second thread displays "Hello" four times,                 */
  33. /*  and the third thread displays "Hello" six times.                          */
  34. /*                                                                            */
  35. /*  In operation, the program works in the following order:                   */
  36. /*                                                                            */
  37. /*     1. Creates the requested number of threads                             */
  38. /*     2. Waits until all threads have been created                           */
  39. /*     3. Begins multithread execution and waits for completion               */
  40. /*                                                                            */
  41. /******************************************************************************/
  42.  
  43. #include <windows.h>
  44. #include <malloc.h>
  45. #include <process.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48.  
  49.  
  50. int main( int argc, char * argv[] );
  51. void childcode( void * arg );
  52.  
  53. typedef struct            /* the thread information structure          */
  54. {                         /*                                           */
  55.    unsigned count;        /* the number of times to display the text   */
  56.    HANDLE   hev  ;        /* the individual thread's event semaphore   */
  57. }  THREAD_INFO ;          /* handle                                    */
  58.  
  59. HANDLE      hev ;            /* the main thread's event semaphore handle  */
  60.  
  61. int main( int argc, char *argv[])
  62. {
  63.    int i, rc;
  64.    int NumThreads;
  65.    THREAD_INFO thread_info[9];
  66.  
  67.    int MaxThread = 0;
  68.  
  69.    if ( argc == 1 ) {
  70.       printf( "*** The syntax is sample02 <arg1> <arg2> ... <up to 9 args>\n");
  71.       return (-1);
  72.    }
  73.  
  74.    if ( argc > 10 ) {
  75.       printf( "*** Error: Too many arguments (maximum 9 arguments)\n");
  76.       return (-1);
  77.    }
  78.  
  79.  
  80.    /*       Create the main thread's event semaphore        */
  81.  
  82.    hev = CreateEvent(NULL,     /*  */
  83.                      TRUE,         /*  */
  84.                      FALSE,           /*  */
  85.                      NULL);       /*  */
  86.                                    /*  */
  87.  
  88.    /*  Create one thread for each argument   */
  89.  
  90.    NumThreads = 1; /* the main thread counts as one */
  91.  
  92.    /*  Create a new thread and pass the corresponding argument  */
  93.  
  94.    for (i=1; i < argc ; i++ ) {
  95.  
  96.     /*       Create an event semaphore for a child thread    */
  97.  
  98.      thread_info[i-1].count  = (unsigned) atol( argv[i] ) ;
  99.  
  100.      thread_info[i-1].hev = CreateEvent(NULL,
  101.                                         FALSE,
  102.                                         FALSE,
  103.                                         NULL);
  104.  
  105.      rc = _beginthread( childcode,
  106.                         NULL,
  107.                         8192,
  108.                         (void *) &thread_info[i-1]);
  109.  
  110.      /* Check for error and keep track of how many threads were created   */
  111.  
  112.      if (rc == -1) {
  113.         printf("*** Error: Could not create %d-th Thread ***\n",
  114.                 NumThreads + 1);
  115.      } else NumThreads++;
  116.  
  117.      if (rc > MaxThread)
  118.         MaxThread = rc;
  119.  
  120.    }
  121.  
  122.    /*      Display how many threads were created         */
  123.  
  124.    printf("Number of threads = %d, Maximum thread ID = %d\n\n",
  125.            NumThreads, MaxThread);
  126.  
  127.    --NumThreads;
  128.  
  129.    /* Let the threads begin execution and wait for them to complete  */
  130.  
  131.    if (FALSE == SetEvent( hev ))
  132.       printf("Last error : %d\n", GetLastError());
  133.  
  134.    for ( i=0; i<NumThreads; ++i) {
  135.       WaitForSingleObject ( thread_info[i].hev, INFINITE);
  136.       CloseHandle ( thread_info[i].hev );
  137.    } /* endfor */
  138.  
  139.    /*  Close the semaphores  */
  140.  
  141.    CloseHandle (hev);
  142.    printf("\nDone!\n");
  143.    return 0;
  144. }
  145.  
  146. /*    "Hello world!" per thread routine.     */
  147.  
  148. void childcode( void * arg)
  149. {
  150.    DWORD     tid ;         /* thread id                                     */
  151.    unsigned int i;        /* index for loop                                */
  152.    THREAD_INFO * thread_info;
  153.  
  154.    thread_info = ( THREAD_INFO * ) arg;
  155.  
  156.    /* Get the thread id.      */
  157.  
  158.  
  159.    tid = GetCurrentThreadId( ) ;
  160.  
  161.    /*   wait for the main routine to say "Go!"   */
  162.  
  163.    WaitForSingleObject ( hev, INFINITE );
  164.  
  165.    /*  print the message <arg> times              */
  166.    /*  using Sleep to delay thread execution      */
  167.  
  168.    for (i = 1; i <=  thread_info->count ; i++ ) {
  169.     printf("Hello world from thread %i!\n",tid);
  170.     Sleep(0L);
  171.    }
  172.  
  173.    /*  Let the main program know everything is done   */
  174.  
  175.    if (FALSE == SetEvent( thread_info->hev ))
  176.       printf("Last error = %d\n", GetLastError());
  177.  
  178. }
  179.