home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / msc51 / example / mhello.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.9 KB  |  132 lines

  1. /*
  2. Multithread "Hello world!"
  3. */
  4.  
  5. #include <mt\malloc.h>
  6. #include <mt\stdio.h>
  7. #include <mt\stdlib.h>
  8. #include <mt\io.h>
  9. #include <mt\stddef.h>  /* definition of _threadid */
  10.  
  11. /* routines */
  12. unsigned far pascal DosSleep ( long millisec ) ;
  13. int main ( int argc , char * * argv ) ;
  14. void childcode ( unsigned far * arg ) ;
  15.  
  16. /* global data */
  17. char Result [ 32 ] ;
  18. char buffer [ 16 ] ;
  19. unsigned Synchronize ;
  20.  
  21. int main ( int argc , char * * argv )
  22. {
  23.     int rc ;
  24.     unsigned result = 0 ;
  25.     char * stackbottom ;
  26.     long ChildCount ;
  27.     int NumThreads ;
  28.     int t ;
  29.     int r ;
  30.     int MaxThread = 0 ;
  31.     long LoopCount ;
  32.  
  33.     if ( -- argc > 31 )
  34.     {
  35.     printf ( "*** Error: Too many arguments***\n" ) ;
  36.     return (-1) ;
  37.     }
  38.  
  39.     /* Bring up one thread for each argument */
  40.  
  41.     NumThreads = 0 ;
  42.  
  43.     while ( * ++ argv )
  44.     {
  45.         /* check for valid arguments */
  46.        
  47.         if (atol (* argv) == 0L)
  48.         {
  49.              printf ("*** Error: Non-integer argument ***\n") ;
  50.              break ;
  51.         }
  52.  
  53.         /* allocate a stack for the next thread */
  54.  
  55.         if ( ! ( stackbottom = _fmalloc ( 2048 ) ) )
  56.         {
  57.               printf ( "*** Error: Could not allocate a stack ***\n" ) ;
  58.               break ;
  59.         }
  60.  
  61.         /* bring up the new thread and pass the corresponding argument */
  62.  
  63.         ChildCount = atol ( * argv ) ;
  64.  
  65.         rc = _beginthread ( childcode , (void far *) stackbottom ,
  66.               2048 , (void far *) ChildCount ) ;
  67.  
  68.         /* Check for error and keep track of how many threads we brought up */
  69.  
  70.         if ( rc == -1 || ChildCount == 0L)
  71.         {
  72.               printf ("*** Error: Could not Spawn %d-th Thread ***\n" ,
  73.               NumThreads + 1) ;
  74.               break ;
  75.         }
  76.         else
  77.               NumThreads++;
  78.  
  79.         if ( rc > MaxThread )
  80.               MaxThread = rc ;
  81.  
  82.     }
  83.  
  84.     /* Tell the user how many threads we brought up */
  85.  
  86.     printf ( "Number of threads = %d, Maximum thread ID = %d\n\n" ,
  87.     NumThreads, MaxThread ) ;
  88.  
  89.     /* Let the threads begin execution  and wait for them to complete */
  90.  
  91.     LoopCount = 0L ;
  92.     Synchronize = 1 ;
  93.  
  94.     for ( t = 0 ; t < NumThreads ; ++ t )
  95.     {
  96.         r = 0 ;
  97.         while ( ! Result [ r ] )
  98.         {
  99.               DosSleep ( 0L ) ;
  100.               if ( ++ r > MaxThread )
  101.                    r = 0 ;
  102.         }
  103.  
  104.         Result [ r ] = '\0' ;
  105.     }
  106.  
  107.     printf("\nDone!\n");
  108.     return 0 ;
  109. }
  110.  
  111.  
  112. /*
  113. "Hello world!" per thread routine.
  114. */
  115.  
  116. void childcode ( unsigned far * arg )
  117. {
  118.     int tid = * _threadid ;
  119.     int i;
  120.  
  121.     /* wait for the main routine to say "Go!" */
  122.     while ( ! Synchronize )
  123.         DosSleep ( 0L ) ;
  124.  
  125.     /* Print the message <arg> times */
  126.     for (i=1; i<= (int)arg; i++)
  127.         printf("Hello world from thread %i!\n", tid);
  128.  
  129.     /* Let the main program know we're done. */
  130.     Result [ tid ] = (char) arg ;
  131. }
  132.