home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / THDPL.ZIP / THRDTEST.CPP < prev    next >
C/C++ Source or Header  |  1992-02-02  |  5KB  |  151 lines

  1. /************************************************************************
  2.  
  3.       File Name..........: THRDPOOL.CPP
  4.       File Description...: Thread Pool Test Program
  5.       Date Written.......: 2-Feb-1992
  6.       Written By.........: Steve Horne / CoralSoft, Inc.
  7.                            305 Judson Dr. E.
  8.                            Mobile, AL  36608
  9.                            (205) 344-2251
  10.  
  11. -------------------------------------------------------------------------
  12.  
  13.    $Log:   E:/CSICLASS/SOURCE/VCS/THRDTEST.CVP  $
  14.    
  15.       Rev 1.0   02 Feb 1992 18:54:26
  16.    Initial revision.
  17.  
  18. -------------------------------------------------------------------------
  19.  
  20.    The C and C++ structures and code in this document have been created
  21.    by CoralSoft, Inc. and are considered proprietary and confidential.
  22.    This information may not be distributed by any means, electronic or
  23.    mechanical, without the prior written consent of CoralSoft, Inc.
  24.    No warranties are either EXPRESSED or IMPLIED.
  25.    Copyright (c) 1991, CoralSoft, Inc.
  26.  
  27. ************************************************************************/
  28. #define   INCL_BASE
  29. #define   INCL_NOPM
  30. #include   <os2.h>
  31. #include   <stdlib.h>
  32. #include <process.h>
  33. #include <disp.h>
  34. #include "thrdpool.hpp"
  35.  
  36. void test_thread(PVOID arg);
  37.  
  38. //   'global_sem' is used to coordinate use of the "DISP" package between
  39. //   the threads.
  40. ULONG global_sem = 0L;
  41.  
  42. const int thread_column = 22;
  43. typedef struct _THREADDATA {
  44.    int tid;
  45.    int count;
  46.    ULONG delay;
  47.    int row;
  48. } THREADDATA;
  49.  
  50. main()
  51. {
  52.    ULONG tt_sem1 = 0L;
  53.    ULONG tt_sem2 = 0L;
  54.    ULONG tt_sem3 = 0L;
  55.  
  56.    disp_open();                           // open display package
  57.    disp_move(0,0);                        // and clear screen
  58.    disp_eeop();
  59.    disp_box(0, DISP_NORMAL, 1, 1, 7, 40);   // box around thread output
  60.    disp_move(2, 5);
  61.    disp_printf("Thread 1 output:");
  62.    disp_move(4, 5);
  63.    disp_printf("Thread 2 output:");
  64.    disp_move(6, 5);
  65.    disp_printf("Thread 3 output:");
  66.  
  67. //   Initialize the ThreadPool object
  68.    ThreadPool pool(5);
  69.  
  70. //   Initialize the termination semaphores for all three threads
  71.    DosSemSet(&tt_sem1);
  72.    DosSemSet(&tt_sem2);
  73.    DosSemSet(&tt_sem3);
  74.  
  75. //   Create & initialize THREADDATA structures for the threads to use
  76. //   as arguments.
  77.    THREADDATA data1 = {1,  5, 250L, 2};
  78.    THREADDATA data2 = {2, 10, 250L, 4};
  79.    THREADDATA data3 = {3, 15, 250L, 6};
  80.  
  81. //   Start thread 1
  82.    USHORT rc= pool.start(test_thread, (PVOID) &data1, &tt_sem1);
  83.    if (rc)
  84.       disp_printf("\nERROR %d in pool.start()", rc);
  85.  
  86. //   Start thread 2
  87.    rc= pool.start(test_thread, (PVOID) &data2, &tt_sem2);
  88.    if (rc)
  89.       disp_printf("\nERROR %d in pool.start()", rc);
  90.  
  91. //   Start thread 3
  92.    rc= pool.start(test_thread, (PVOID) &data3, &tt_sem3);
  93.    if (rc)
  94.       disp_printf("\nERROR %d in pool.start()", rc);
  95.  
  96.    DosSemRequest(&global_sem, SEM_INDEFINITE_WAIT);
  97.    disp_move(2, 45);
  98.    disp_printf("MAIN waiting...");
  99.    DosSemClear(&global_sem);
  100.    DosSemWait(&tt_sem1, SEM_INDEFINITE_WAIT);
  101.    DosSemRequest(&global_sem, SEM_INDEFINITE_WAIT);
  102.    disp_move(2, 45);
  103.    disp_printf("Finished.......");
  104.    disp_move(4, 45);
  105.    disp_printf("MAIN waiting...");
  106.    DosSemClear(&global_sem);
  107.    DosSemWait(&tt_sem2, SEM_INDEFINITE_WAIT);
  108.    DosSemRequest(&global_sem, SEM_INDEFINITE_WAIT);
  109.    disp_move(4, 45);
  110.    disp_printf("Finished.......");
  111.    disp_move(6, 45);
  112.    disp_printf("MAIN waiting...");
  113.    DosSemClear(&global_sem);
  114.    DosSemWait(&tt_sem3, SEM_INDEFINITE_WAIT);
  115.    disp_move(6, 45);
  116.    disp_printf("Finished.......");
  117.    disp_move(9, 0);
  118.    disp_printf("Main has finished... All threads done.");
  119.    disp_close();
  120.    return(0);
  121. }
  122.  
  123. //   'test_thread' is the function executed by each of the threads started...
  124. //   Notice that this is a C++ function, not C.
  125. //   The threads do different things depending on the argument passed, which
  126. //   is actually a pointer to a THREADDATA structure.
  127.  
  128. void test_thread(PVOID arg)
  129. {
  130. THREADDATA *td;
  131.  
  132.    td= (THREADDATA *) arg;
  133.    for (int cnt= 0; cnt < td->count; cnt++)
  134.       {
  135.       DosSleep(td->delay);
  136.       DosSemRequest(&global_sem, SEM_INDEFINITE_WAIT);
  137.       disp_move(td->row, thread_column + cnt);
  138.       disp_printf("+");
  139.       disp_flush();
  140.       DosSemClear(&global_sem);
  141.       }
  142.    DosSemRequest(&global_sem, SEM_INDEFINITE_WAIT);
  143.    disp_move(td->row, thread_column + cnt);
  144.    disp_printf("*");
  145.    disp_flush();
  146.    DosSemClear(&global_sem);
  147. }
  148.  
  149.  
  150.  
  151.