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

  1. //--------------------------------------------------------------------------
  2. //
  3. //      EXAMPLE2.CPP: example program 2 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 Example2.
  25. //
  26. //      This is a coroutine which displays a series of messages.  It
  27. //      illustrates how the main program can terminate the coroutines
  28. //      it creates in an orderly manner.
  29. //
  30. class Example2 : public Coroutine
  31. {
  32.   public:
  33.     Example2 (int n)        { num = n; }
  34.  
  35.   protected:
  36.     virtual void main ();   // code to be executed by coroutine
  37.  
  38.   private:
  39.     int num;                // coroutine identification number
  40. };
  41.  
  42. static int quit = 0;        // flag to request coroutine termination
  43.  
  44. //--------------------------------------------------------------------------
  45. //
  46. //      Example2::main.
  47. //
  48. //      This is the code executed by each instance of class Example2.
  49. //      It displays a startup message, executes a loop (pausing each
  50. //      time) until the main program sets the "quit" flag, and then
  51. //      displays a termination message before exiting.
  52. //
  53. void Example2::main ()
  54. {
  55.     cout << "\nCoroutine E" << num << " started\n";
  56.  
  57.     while (!quit)
  58.     {   cout << num << " ";
  59.         pause ();
  60.     }
  61.  
  62.     cout << "\nCoroutine E" << num << " finished\n";
  63. }
  64.  
  65. //--------------------------------------------------------------------------
  66. //
  67. //      The main program.
  68. //
  69. //      This creates three instances of class Example2, starts them
  70. //      running and then waits until ESC is pressed.  It then sets
  71. //      the "quit" flag to signal the coroutines to terminate before
  72. //      exiting.  Note that the loop which waits for ESC to be pressed
  73. //      calls "pause" each time to allow the coroutines to execute, and
  74. //      that it uses bioskey(1) (test if a key has been pressed) rather
  75. //      than just bioskey(0) which would wait for a keypress and would
  76. //      not allow the couroutines to run between keypresses.
  77. //
  78. void main ()
  79. {
  80.     Example2 e1 (1), e2 (2), e3 (3);
  81.  
  82.     //--- start the coroutines running
  83.     if (!e1.run ())
  84.         cout << "Couldn't start e1\n";
  85.     if (!e2.run ())
  86.         cout << "Couldn't start e2\n";
  87.     if (!e3.run ())
  88.         cout << "Couldn't start e3\n";
  89.  
  90.     //--- wait for ESC to be pressed
  91.     while (bioskey (1) == 0 || (bioskey (0) & 0xFF) != ESC)
  92.         Example2::pause ();
  93.  
  94.     //--- terminate the coroutines
  95.     quit = 1;
  96.  
  97. }   //--- destructors called here: wait for coroutines to finish, then exit
  98.  
  99.