home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / os2thred.zip / SAMPLE.CPP < prev    next >
C/C++ Source or Header  |  1994-06-09  |  3KB  |  130 lines

  1. // Listing 11 -- Sample.cpp
  2.  
  3. //------------------------- Includes ------------------------------
  4.  
  5. #define INCL_DOS
  6. #include <os2.h>
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "MsgThrd.h"
  11.  
  12. //-------------------------- defines ------------------------------
  13.  
  14. const USHORT NUMTHREADS                = 10;
  15.  
  16. const ULONG MSGID_STAMP_MESSAGE        = MSG_THRD_USER;
  17. const ULONG MSGID_DISPLAY            = MSGID_STAMP_MESSAGE + 1;
  18.  
  19. //--------------------------- types -------------------------------
  20.  
  21. struct instanceData {
  22.     ULONG        id;
  23.     MsgThread*    pNxtQ;
  24.     MsgThread*    pLastQ;
  25. };
  26.  
  27. static instanceData thrdData[NUMTHREADS];
  28.  
  29. //---------------------- local prototypes -------------------------
  30.  
  31. VOID threadProc (ULONG objAddr, ULONG msgID,
  32.                       MPARAM mp1, MPARAM mp2, ULONG ulParam);
  33. VOID lastThreadProc (ULONG objAddr, ULONG msgID,
  34.                             MPARAM mp1, MPARAM mp2, ULONG ulParam);
  35.  
  36. //--------------------------- code --------------------------------
  37.  
  38. void main ()
  39. {
  40.     HEV    hevDone;
  41.     MsgThread    lastThrd(lastThreadProc);
  42.     
  43.     DosCreateEventSem(NULL, &hevDone, 0L, FALSE);
  44.     lastThrd.Start((ULONG)hevDone);
  45.  
  46.     //------------------------------------------
  47.     // Create Threads
  48.     MsgThread*        msgThrd[NUMTHREADS];
  49.     USHORT            i;
  50.     for (i=0; i < NUMTHREADS; i++)
  51.         msgThrd[i] = new MsgThread(threadProc);
  52.  
  53.     //------------------------------------------
  54.     // Start the threads
  55.     ULONG        idStamp = 1L;
  56.     MsgThread*   pNxtQ;
  57.     for (i=0; i < NUMTHREADS; i++) {
  58.         if (i < (NUMTHREADS-1))
  59.             pNxtQ = msgThrd[i+1];
  60.         else
  61.             pNxtQ = msgThrd[0];
  62.         thrdData[i].id = idStamp;
  63.         thrdData[i].pNxtQ = pNxtQ;
  64.         thrdData[i].pLastQ = &lastThrd;
  65.         msgThrd[i]->Start((ULONG)(&thrdData[i]));
  66.         idStamp = idStamp << 1;
  67.     }
  68.  
  69.     //------------------------------------------
  70.     // Send a message to each of the threads
  71.     for (i=0; i < NUMTHREADS; i++) {
  72.         msgThrd[i]->SendMsg((ULONG)NULL, MSGID_STAMP_MESSAGE,
  73.                          0L, (MPARAM)thrdData[i].id);
  74.     }
  75.  
  76.     // Wait for all the message threads to terminate
  77.     DosWaitEventSem(hevDone, SEM_INDEFINITE_WAIT);
  78.     printf ("Done waiting for output... destroying threads\n");
  79.  
  80.     for (i=0; i < NUMTHREADS; i++)
  81.         delete(msgThrd[i]);
  82.     exit(0);
  83. }
  84.  
  85. VOID lastThreadProc (ULONG objAddr, ULONG msgID,
  86.                             MPARAM mp1, MPARAM mp2, ULONG ulParam)
  87. {
  88.     static  USHORT        msgCount;
  89.     static  HEV            hevDone;
  90.  
  91.     switch (msgID) {
  92.         case MSG_THRD_STARTUP:
  93.             msgCount = NUMTHREADS;
  94.             hevDone = (HEV)mp1;
  95.             break;
  96.         case MSGID_DISPLAY:
  97.             printf ("Message from %8lx value: %8lx\n",(ULONG)mp2, (ULONG)mp1);
  98.             msgCount--;
  99.             if (msgCount == 0)
  100.                 DosPostEventSem(hevDone);
  101.             break;
  102.         default:
  103.             break;
  104.     }
  105. }
  106.  
  107. VOID threadProc (ULONG objAddr, ULONG msgID,
  108.                         MPARAM mp1, MPARAM mp2, ULONG ulParam)
  109. {
  110.     instanceData*  pData = (instanceData*)ulParam;
  111.  
  112.     switch (msgID) {
  113.         case MSG_THRD_STARTUP:
  114.         case MSG_THRD_SHUTDOWN:
  115.             break;
  116.         case MSGID_STAMP_MESSAGE:
  117.             if ((ULONG)mp1 & pData->id) {
  118.                 pData->pLastQ->SendMsg(objAddr, MSGID_DISPLAY, mp1, mp2);
  119.             } else {
  120.                 mp1 = (MPARAM)((ULONG)mp1 | pData->id);
  121.                 pData->pNxtQ->SendMsg(objAddr, MSGID_STAMP_MESSAGE, mp1, mp2);
  122.             }
  123.             break;
  124.         default:
  125.             break;
  126.     }
  127. }
  128.  
  129. //------------------------ end of file ----------------------------
  130.