home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / SOFTWARE / LIBS / MTL110JE.ZIP / EXAMPLE6.CPP (.txt) < prev    next >
C/C++ Source or Header  |  1993-07-20  |  3KB  |  123 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      EXAMPLE6.CPP: example for DOS multithreading library.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15. //
  16. //      This example is identical to the previous one, except that
  17. //      it uses a monitor to protect against re-entrant DOS and BIOS
  18. //      calls.  Note that we don't want DOS calls to be made while
  19. //      we're inside the monitor, so "DOSlock" and "DOSunlock" are
  20. //      used instead of "lock" and "unlock".
  21. //
  22. //--------------------------------------------------------------------------
  23.  
  24. #include <stdio.h>
  25. #include <bios.h>
  26. #include "threads.h"
  27. #include "buffers.h"
  28.  
  29. BoundedBuffer<char> buffer (20);        // a buffer of 20 characters
  30.  
  31. class BIOSmonitor : public DOSMonitor
  32. {
  33.   public:
  34.     char get ();
  35. };
  36.  
  37. char BIOSmonitor::get ()
  38. {
  39.     DOSlock ();
  40.     char c = bioskey(0) & 0xFF;
  41.     DOSunlock ();
  42.     return c;
  43. }
  44.  
  45. BIOSmonitor bios;
  46.  
  47. class Example6a : public DOSThread
  48. {
  49.   public:
  50.     Example6a ()            { }
  51.     ~Example6a ();
  52.  
  53.   protected:
  54.     virtual void main ();
  55. };
  56.  
  57. void Example6a::main ()
  58. {
  59.     char c;
  60.     for (;;)
  61.     {   if (!buffer.get (c))
  62.             return;
  63.         putchar (c);
  64.     }
  65. }
  66.  
  67. Example6a::~Example6a ()
  68. {
  69.     wait ();
  70.     fputs ("\nEnd of thread A\n", stdout);
  71. }
  72.  
  73. class Example6b : public DOSThread
  74. {
  75.   public:
  76.     Example6b ()            { }
  77.     ~Example6b ();
  78.  
  79.   protected:
  80.     virtual void main ();
  81. };
  82.  
  83. void Example6b::main ()
  84. {
  85.     char c;
  86.     for (;;)
  87.     {   if (!buffer.get (c))
  88.             return;
  89.         putchar ('*');
  90.     }
  91. }
  92.  
  93. Example6b::~Example6b ()
  94. {
  95.     wait ();
  96.     fputs ("\nEnd of thread B\n", stdout);
  97. }
  98.  
  99. void main ()
  100. {
  101.     Example6a A;
  102.     Example6b B;
  103.  
  104.     if (A.run ())
  105.         puts ("Thread A started");
  106.     if (B.run ())
  107.         puts ("Thread B started");
  108.  
  109.     puts ("Press ESC to terminate");
  110.  
  111.     char c;
  112.     for (;;)
  113.     {   c = bios.get ();
  114.         if (c == 0x1B)
  115.         {   buffer.close ();
  116.             puts ("\nTerminating...");
  117.             break;
  118.         }
  119.         else
  120.             buffer.put (c);
  121.     }
  122. }
  123.