home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / CCL110JE.ZIP / EXAMPLE3.CPP < prev    next >
C/C++ Source or Header  |  1993-06-19  |  4KB  |  132 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      EXAMPLE3.CPP: example program 3 for DOS coroutine 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. #include <iostream.h>
  17. #include <bios.h>
  18. #include "coroutine.h"
  19.  
  20. #define ESC     0x1B        // code for the ESC key
  21.  
  22. //--------------------------------------------------------------------------
  23. //
  24. //      Class Example3.
  25. //
  26. //      This is a coroutine which displays a series of messages.  It
  27. //      illustrates how "terminate" can be used as well as the use of
  28. //      "wait" in a coroutine destructor.
  29. //
  30. class Example3 : public Coroutine
  31. {
  32.   public:
  33.     Example3 (int n)        { num = n; }
  34.     ~Example3 ();
  35.  
  36.   protected:
  37.     virtual void main ();   // code to be executed by coroutine
  38.  
  39.   private:
  40.     int num;                // coroutine identification number
  41. };
  42.  
  43. static int quit = 0;        // flag to request coroutine termination
  44.  
  45. //--------------------------------------------------------------------------
  46. //
  47. //      Example3::~Example3.
  48. //
  49. //      The coroutine class destructor.  Note how "wait" is called at the
  50. //      beginning of the destructor to ensure the coroutine has terminated
  51. //      before any other finalisation is done.
  52. //
  53. Example3::~Example3 ()
  54. {
  55.     wait ();
  56.     cout << "\nCoroutine E" << num << " finished\n";
  57. }
  58.  
  59. //--------------------------------------------------------------------------
  60. //
  61. //      Example3::main.
  62. //
  63. //      This is the code executed by each instance of class Example3.
  64. //      It displays a startup message, executes a loop (pausing each
  65. //      time) until the main program sets the "quit" flag, and then
  66. //      displays a termination message before exiting.
  67. //
  68. void Example3::main ()
  69. {
  70.     cout << "\nCoroutine E" << num << " started\n";
  71.  
  72.     while (!quit)
  73.     {   cout << num << " ";
  74.         pause ();
  75.     }
  76. }
  77.  
  78. //--------------------------------------------------------------------------
  79. //
  80. //      The main program.
  81. //
  82. //      This creates three instances of class Example3, starts them
  83. //      running and then waits until ESC is pressed.  It then sets
  84. //      the "quit" flag to signal the coroutines to terminate before
  85. //      exiting.  If 1, 2 or 3 is pressed, the corresponding coroutine
  86. //      is immediately terminated.  Note that the loop which waits for
  87. //      a key to be pressed calls "pause" each time to allow the coroutines
  88. //      to execute, and that it uses bioskey(1) (test if a key has been
  89. //      pressed) rather than just bioskey(0) which would wait for a key
  90. //      to be pressed and would not allow the coroutines to run between
  91. //      keypresses.
  92. //
  93. void main ()
  94. {
  95.     Example3 e1 (1), e2 (2), e3 (3);
  96.  
  97.     //--- announce startup
  98.     cout << "Press ESC to exit, 1 or 2 or 3 to terminate the corresponding\n"
  99.          << "coroutine.  Press any key to start.\n";
  100.     bioskey (0);
  101.  
  102.     //--- start the coroutines running
  103.     if (!e1.run ())
  104.         cout << "Couldn't start e1\n";
  105.     if (!e2.run ())
  106.         cout << "Couldn't start e2\n";
  107.     if (!e3.run ())
  108.         cout << "Couldn't start e3\n";
  109.  
  110.     //--- wait for a suitable key to be pressed
  111.     for (;;)
  112.     {   while (bioskey (1) == 0)
  113.             Example3::pause ();
  114.         switch (bioskey (0) & 0xFF)
  115.         {
  116.             case ESC:
  117.                 quit = 1;
  118.                 return;
  119.             case '1':
  120.                 e1.terminate ();
  121.                 break;
  122.             case '2':
  123.                 e2.terminate ();
  124.                 break;
  125.             case '3':
  126.                 e3.terminate ();
  127.                 break;
  128.         }
  129.     }
  130. }   //--- destructors called here: wait for coroutines to finish, then exit
  131.  
  132.