home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / COMPILER / SAMPLE02 / SAMPLE02.C < prev    next >
Text File  |  1993-03-15  |  8KB  |  184 lines

  1. /******************************************************************************/
  2. /* SAMPLE PROGRAM 02: SAMPLE02.C                                              */
  3. /*                                                                            */
  4. /* COPYRIGHT:                                                                 */
  5. /* ----------                                                                 */
  6. /* Copyright (C) International Business Machines Corp., 1991,1993.            */
  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 C Set/2 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. /*  NOTE:  The program also requires the IBM TOOLKIT 2.0 headers              */
  42. /*                                                                            */
  43. /******************************************************************************/
  44.  
  45.  
  46. #define  INCL_DOS
  47. #include <os2.h>
  48. #include <malloc.h>
  49. #include <process.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52.  
  53.  
  54. int main( int argc, char * argv[] );
  55. void childcode( void * arg );
  56.  
  57. typedef struct            /* the thread information structure          */
  58. {                         /*                                           */
  59.    unsigned count;        /* the number of times to display the text   */
  60.    HEV      hev  ;        /* the individual thread's event semaphore   */
  61. }  THREAD_INFO ;          /* handle                                    */
  62.  
  63. HEV      hev ;            /* the main thread's event semaphore handle  */
  64.  
  65. int main( int argc, char *argv[])
  66. {
  67.    int i, rc;
  68.    int NumThreads;
  69.    THREAD_INFO thread_info[9];
  70.  
  71.    int MaxThread = 0;
  72.  
  73.    if ( argc == 1 ) {
  74.       printf( "*** The syntax is sample01 <agr1> <arg2> ... <up to 9 args>\n");
  75.       return (-1);
  76.    }
  77.  
  78.    if ( argc > 10 ) {
  79.       printf( "*** Error: Too many arguments (maximum 9 arguments)\n");
  80.       return (-1);
  81.    }
  82.  
  83.  
  84.    /*       Create the main thread's event semaphore        */
  85.  
  86.    DosCreateEventSem(NULL,         /* this is an anonymous semaphore          */
  87.                      &hev,         /* a pointer to receive a semaphore handle */
  88.                      0L,           /* this is a private semaphore             */
  89.                      FALSE);       /* initial state of the semaphore is       */
  90.                                    /* owned.                                  */
  91.  
  92.    /*  Create one thread for each argument   */
  93.  
  94.    NumThreads = 1; /* the main thread counts as one */
  95.  
  96.    /*  Create a new thread and pass the corresponding argument  */
  97.  
  98.    for (i=1; i < argc ; i++ ) {
  99.  
  100.     /*       Create an event semaphore for a child thread    */
  101.  
  102.      thread_info[i-1].count  = (unsigned) atol( argv[i] ) ;
  103.  
  104.      DosCreateEventSem(NULL,
  105.                        &thread_info[i-1].hev,
  106.                        0L,
  107.                        FALSE);
  108.  
  109.      rc = _beginthread( childcode,
  110.                         NULL,
  111.                         8192,
  112.                         (void *) &thread_info[i-1]);
  113.  
  114.      /* Check for error and keep track of how many threads were created   */
  115.  
  116.      if (rc == -1) {
  117.         printf("*** Error: Could not create %d-th Thread ***\n",
  118.                 NumThreads + 1);
  119.      } else NumThreads++;
  120.  
  121.      if (rc > MaxThread)
  122.         MaxThread = rc;
  123.  
  124.    }
  125.  
  126.    /*      Display how many threads were created         */
  127.  
  128.    printf("Number of threads = %d, Maximum thread ID = %d\n\n",
  129.            NumThreads, MaxThread);
  130.  
  131.    --NumThreads;
  132.  
  133.    /* Let the threads begin execution and wait for them to complete  */
  134.  
  135.    DosPostEventSem( hev );
  136.  
  137.    for ( i=0; i<NumThreads; ++i) {
  138.       DosWaitEventSem ( thread_info[i].hev, SEM_INDEFINITE_WAIT  );
  139.       DosCloseEventSem( thread_info[i].hev );
  140.    } /* endfor */
  141.  
  142.    /*  Close the semaphores  */
  143.  
  144.    DosCloseEventSem (hev);
  145.    printf("\nDone!\n");
  146.    return 0;
  147. }
  148.  
  149. /*    "Hello world!" per thread routine.     */
  150.  
  151. void childcode( void * arg)
  152. {
  153.    TIB  *ptib;            /* pointer to a thread information block         */
  154.    PIB  *ppib;            /* pointer to a process information block        */
  155.    unsigned tid ;         /* thread id                                     */
  156.    unsigned int i;        /* index for loop                                */
  157.    THREAD_INFO * thread_info;
  158.  
  159.    thread_info = ( THREAD_INFO * ) arg;
  160.  
  161.    /* Get the addresses of the thread and process information blocks.      */
  162.  
  163.    DosGetInfoBlocks( &ptib, &ppib );
  164.  
  165.    tid = ( unsigned ) ptib->tib_ptib2->tib2_ultid ;
  166.  
  167.    /*   wait for the main routine to say "Go!"   */
  168.  
  169.    DosWaitEventSem ( hev, SEM_INDEFINITE_WAIT  );
  170.  
  171.    /*  print the message <arg> times              */
  172.    /*  using DosSleep to delay thread execution   */
  173.  
  174.    for (i = 1; i <=  thread_info->count ; i++ ) {
  175.     printf("Hello world from thread %i!\n",tid);
  176.     DosSleep(0L);
  177.    }
  178.  
  179.    /*  Let the main program know everything is done   */
  180.  
  181.    DosPostEventSem( thread_info->hev );
  182.  
  183. }
  184.