home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12225.ZIP / THREADS.C < prev    next >
C/C++ Source or Header  |  1988-08-18  |  5KB  |  173 lines

  1. /*----------------------------------------------------------
  2.    THREADS.C -- Multi-Thread OS/2 Program for CodeView Demo
  3.                 Programmed by Charles Petzold, 6/88
  4.   ----------------------------------------------------------*/
  5.  
  6. #define INCL_DOS
  7. #define INCL_VIO
  8. #define INCL_KBD
  9.  
  10. #define INCL_SUB
  11.  
  12. #include <os2.h>
  13. #include <mt\process.h>
  14. #include <mt\stdlib.h>
  15. #include <mt\stddef.h>
  16.  
  17.      /*------------------------------------------------
  18.         Structure definition and function declarations
  19.        ------------------------------------------------*/
  20.  
  21. typedef struct
  22.      {
  23.      SHORT xLeft ;
  24.      SHORT yTop ;
  25.      SHORT xRight ;
  26.      SHORT yBottom ;
  27.      }
  28.      THREADPARAM ;
  29.  
  30. typedef THREADPARAM FAR *PTHREADPARAM ;
  31.  
  32. BOOL            CheckKey       (CHAR ch) ;
  33. VOID FAR ThreadFunction (PTHREADPARAM ptp) ;
  34. VOID            WriteChar      (CHAR ch, USHORT usRow, USHORT usCol) ;
  35. VOID FAR ExitRoutine    (USHORT usTermCode) ;
  36. VOID            ClearScreen    (BOOL fCursorOff) ;
  37.  
  38.      /*-----------------
  39.         main() function
  40.        -----------------*/
  41.  
  42. INT _CDECL main (VOID)
  43.      {
  44.      static INT  iStack [4][2048] ;
  45.      static CHAR szErrorMsg[] = "THREADS: Cannot start thread" ;
  46.      KBDKEYINFO  kbci ;
  47.      SHORT       i ;
  48.      THREADPARAM tp[4] ;
  49.      VIOMODEINFO viomi ;
  50.  
  51.           /*---------------------------------------------
  52.              Clear screen and get video mode information
  53.             ---------------------------------------------*/
  54.  
  55.      DosExitList (EXLST_ADD, ExitRoutine) ;
  56.      ClearScreen (TRUE) ;
  57.  
  58.      viomi.cb = sizeof viomi ;
  59.      VioGetMode (&viomi, 0) ;
  60.  
  61.           /*-----------------------
  62.              Start up four threads
  63.             -----------------------*/
  64.  
  65.      for (i = 0 ; i < 4 ; i++)
  66.           {
  67.           tp[i].xLeft = i % 2 ? viomi.col / 2 : 0 ;
  68.           tp[i].yTop  = i > 1 ? viomi.row / 2 : 0 ;
  69.  
  70.           tp[i].xRight  = tp[i].xLeft + viomi.col / 2 ;
  71.           tp[i].yBottom = tp[i].yTop  + viomi.row / 2 ;
  72.  
  73.           if (-1 == _beginthread (ThreadFunction, iStack[i],
  74.                                   sizeof iStack[i], tp + i))
  75.                {
  76.                VioWrtTTy (szErrorMsg, sizeof szErrorMsg - 1, 0) ;
  77.                return 1 ;
  78.                }
  79.           }
  80.  
  81.           /*--------------------------
  82.              Wait for Esc key to exit
  83.             --------------------------*/
  84.      do
  85.           KbdCharIn (&kbci, IO_WAIT, 0) ;
  86.      while (!CheckKey (kbci.chChar)) ;
  87.  
  88.      return 0 ;
  89.      }
  90.  
  91.      /*-----------------------------------------------------
  92.         CheckKey() function -- Returns TRUE if ch is Escape
  93.        -----------------------------------------------------*/
  94.  
  95. BOOL CheckKey (CHAR ch)
  96.      {
  97.      return ch == '\33' ;
  98.      }
  99.  
  100.      /*------------------
  101.         ThreadFunction()
  102.        ------------------*/
  103.  
  104. VOID FAR ThreadFunction (PTHREADPARAM ptp)
  105.      {
  106.      CHAR   ch ;
  107.      SEL    selGlobal, selLocal ;
  108.      TID    tid ;
  109.      USHORT usRow, usCol ;
  110.  
  111.           /*----------------------------------------
  112.              Get thread ID and convert to character
  113.             ----------------------------------------*/
  114.  
  115.      DosGetInfoSeg (&selGlobal, &selLocal) ;
  116.      tid = ((PLINFOSEG) MAKEP (selLocal, 0)) -> tidCurrent ;
  117.      ch  = (CHAR) tid + '0' ;
  118.      srand (tid) ;
  119.  
  120.           /*---------------------------------------
  121.              Display thread ID in random locations
  122.             ---------------------------------------*/
  123.  
  124.      while (TRUE)
  125.           {
  126.           usCol = ptp->xLeft + rand () % (ptp->xRight - ptp->xLeft) ;
  127.           usRow = ptp->yTop  + rand () % (ptp->yBottom - ptp->yTop) ;
  128.           WriteChar (ch, usRow, usCol) ;
  129.           }
  130.      }
  131.  
  132.      /*----------------------------------------------------------
  133.         WriteChar() function -- Displays TID, pauses, then blank
  134.        ----------------------------------------------------------*/
  135.  
  136. VOID WriteChar (CHAR ch, USHORT usRow, USHORT usCol)
  137.      {
  138.      VioWrtCharStr (&ch, 1, usRow, usCol, 0) ;
  139.      DosSleep (100L) ;
  140.      VioWrtCharStr (" ", 1, usRow, usCol, 0) ;
  141.      }
  142.  
  143.      /*------------------------------------------------
  144.         ExitRoutine() function -- Exit list processing
  145.        ------------------------------------------------*/
  146.  
  147. VOID FAR ExitRoutine (USHORT usTermCode)
  148.      {
  149.      usTermCode ;   /* to prevent compiler warning */
  150.  
  151.      ClearScreen (FALSE) ;
  152.      DosExitList (EXLST_EXIT, NULL) ;
  153.      }
  154.  
  155.      /*--------------------------------------------------------
  156.         ClearScreen() function -- Also turns cursor off and on
  157.        --------------------------------------------------------*/
  158.  
  159. VOID ClearScreen (BOOL fCursorOff)
  160.      {
  161.      USHORT        usAnsi ;
  162.      VIOCURSORINFO vioci ;
  163.  
  164.      VioGetAnsi (&usAnsi, 0) ;
  165.      VioSetAnsi (ANSI_ON, 0) ;
  166.      VioWrtTTy ("\33[2J", 4, 0) ;
  167.      VioSetAnsi (usAnsi, 0) ;
  168.  
  169.      VioGetCurType (&vioci, 0) ;
  170.      vioci.attr = fCursorOff ? -1 : 0 ;
  171.      VioSetCurType (&vioci, 0) ;
  172.      }
  173.