home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PP0706.ZIP / ALPHSOUP.C next >
C/C++ Source or Header  |  1988-03-29  |  3KB  |  98 lines

  1. /*---------------------------------------------------------------------------
  2.    ALPHSOUP.C -- OS/2 Program that Runs 26 Threads Using One Thread Function
  3.                  (C) 1988, Ziff-Davis Communications Company
  4.                  PC Magazine * Programmed by Charles Petzold, 11/87
  5.   ---------------------------------------------------------------------------*/
  6.  
  7. #include <doscalls.h>
  8. #include <subcalls.h>
  9.  
  10. void far ThreadFunction (void) ;
  11.  
  12. main ()
  13.      {
  14.      static unsigned char ThreadStack [26][1024] ;
  15.      unsigned int         i, ThreadID [26] ;
  16.      struct KeyData       kd ;
  17.  
  18.      for (i = 0 ; i < 26 ; i++)
  19.           if (DOSCREATETHREAD (ThreadFunction, &ThreadID [i],
  20.                                ThreadStack [i] + 1024))               {
  21.                puts ("RANDQUAD: Could not create thread") ;
  22.                return 1 ;
  23.                }
  24.  
  25.      KBDCHARIN (&kd, 0, 0) ;
  26.  
  27.      return 0 ;
  28.      }
  29.  
  30. #pragma check_stack-
  31.  
  32. void far ThreadFunction ()
  33.      {
  34.      static struct ModeData md ;
  35.      static char            ClearCell [2] = " \x07" ;
  36.      static int             ThreadNumber = 0 ;
  37.      unsigned int           MinRow, MaxRow, MinCol, MaxCol ;
  38.      int                    MyThreadNum, Row, Col ;
  39.  
  40.      DOSENTERCRITSEC () ;
  41.  
  42.      if (ThreadNumber == 0)
  43.           {
  44.           VIOSCROLLUP (0, 0, 0xFFFF, 0xFFFF, 0xFFFF, ClearCell, 0L) ;
  45.  
  46.           md.length = sizeof (md) ;
  47.           VIOGETMODE (&md, 0) ;
  48.           }
  49.  
  50.      MyThreadNum = ThreadNumber ;
  51.  
  52.      ThreadNumber += 1 ;
  53.  
  54.      DOSEXITCRITSEC () ;
  55.  
  56.      Row = SafeRand () % md.row ;
  57.      Col = SafeRand () % md.col ;
  58.  
  59.      while (1)
  60.           {
  61.           Row = (Row + SafeRand () % 3 - 1 + md.row) % md.row ;
  62.           Col = (Col + SafeRand () % 3 - 1 + md.col) % md.col ;
  63.  
  64.           Display (0, Row, Col, MyThreadNum) ;
  65.  
  66.           DOSSLEEP (0L) ;
  67.  
  68.           Display (1, Row, Col, MyThreadNum) ;
  69.           }
  70.      }
  71.  
  72. SafeRand ()
  73.      {     static long Semaphore = 0 ;
  74.      int         ReturnValue ;
  75.  
  76.      DOSSEMREQUEST ((unsigned long) (long far *) &Semaphore, -1L) ;
  77.  
  78.      ReturnValue = rand () ;
  79.  
  80.      DOSSEMCLEAR ((unsigned long) (long far *) &Semaphore) ;
  81.  
  82.      return ReturnValue ;
  83.      }
  84.  
  85. Display (Cycle, Row, Col, Num)
  86.      int Cycle, Row, Col, Num ;
  87.      {
  88.      char String [2] ;
  89.  
  90.      String [0] = (char) (Cycle == 0 ? Num + 'A' : ' ') ;
  91.      String [1] = '\x07' ;
  92.  
  93.      if (Num == 0)
  94.           String [1] = '\x1B' ;
  95.  
  96.      VIOWRTCELLSTR (String, 2, Row, Col, 0) ;
  97.      }
  98.