home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / PowerPlant / LProgressIndicator & Friends / Threads App Source / CThreadWindow.cp < prev    next >
Encoding:
Text File  |  1996-04-19  |  3.1 KB  |  105 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CThreadWindow.cp                     ©1996 Metrowerks Inc. All rights reserved.
  3. // =================================================================================
  4.  
  5. #include <LLink.h>
  6. #include <LSharedQueue.h>
  7. #include <LStream.h>
  8.  
  9. #include "ThreadsConstants.h"
  10. #include "CConsumerThread.h"
  11. #include "CProducerThread.h"
  12. #include "LThermometerPane.h"
  13. #include "CVisualSharedQueue.h"
  14.  
  15. #include "CThreadWindow.h"
  16.  
  17.  
  18. // ---------------------------------------------------------------------------------
  19. //        • CreateThreadWindowStream [static]
  20. // ---------------------------------------------------------------------------------
  21.  
  22. CThreadWindow *
  23. CThreadWindow::CreateThreadWindowStream(
  24.     LStream    *inStream )
  25. {
  26.     return new CThreadWindow( inStream );
  27. }
  28.  
  29.  
  30. // ---------------------------------------------------------------------------------
  31. //        • CThreadWindow(LStream*)
  32. // ---------------------------------------------------------------------------------
  33.  
  34. CThreadWindow::CThreadWindow(
  35.     LStream    *inStream )
  36.         : LWindow( inStream ), mSharedQueue( nil ),
  37.             mProducerThread( nil ), mConsumerThread( nil )
  38. {
  39. }
  40.  
  41.  
  42. // ---------------------------------------------------------------------------------
  43. //        • ~CThreadWindow
  44. // ---------------------------------------------------------------------------------
  45.  
  46. CThreadWindow::~CThreadWindow()
  47. {
  48.     // Delete the producer thread.
  49. //    if ( mProducerThread != nil ) mProducerThread->DeleteThread();
  50.     
  51.     // Delete the consumer thread.
  52. //    if ( mConsumerThread != nil ) mConsumerThread->DeleteThread();
  53.     
  54.     if ( mSharedQueue != nil ) {
  55.     
  56.         // Delete remaining items in the queue. Use NextGet instead
  57.         // of Next to avoid the semaphore behavior of the shared queue.
  58.         // Note: another way to do this is with an LQueueIterator proc.
  59.         LLink    *theLink;
  60.         while ( (theLink = mSharedQueue->NextGet()) != nil ) delete theLink;
  61.  
  62.         // Delete the queue.
  63.         delete mSharedQueue;
  64.     
  65.     }
  66. }
  67.  
  68.  
  69. // ---------------------------------------------------------------------------------
  70. //        • FinishCreateSelf
  71. // ---------------------------------------------------------------------------------
  72.  
  73. void
  74. CThreadWindow::FinishCreateSelf()
  75. {
  76.     // Get the queue's progress pane.
  77.     LThermometerPane    *theProgressPane;
  78.     theProgressPane = (LThermometerPane *) FindPaneByID( kSharedQueueProgressPane );
  79.     Assert_( theProgressPane != nil );
  80.  
  81.     // Create the shared queue.
  82.     mSharedQueue = new CVisualSharedQueue( theProgressPane );
  83.     ThrowIfNil_( mSharedQueue );
  84.  
  85.     // Get the producer's progress pane.
  86.     theProgressPane = (LThermometerPane *) FindPaneByID( kProducerProgressPane );
  87.     Assert_( theProgressPane != nil );
  88.     
  89.     // Create the producer thread.
  90.     mProducerThread = new CProducerThread( mSharedQueue, theProgressPane );
  91.     ThrowIfNil_( mProducerThread );
  92.  
  93.     // Get the consumer's progress pane.
  94.     theProgressPane = (LThermometerPane *) FindPaneByID( kConsumerProgressPane );
  95.     Assert_( theProgressPane != nil );
  96.  
  97.     // Create the consumer thread.
  98.     mConsumerThread = new CConsumerThread( mSharedQueue, theProgressPane );
  99.     ThrowIfNil_( mConsumerThread );
  100.  
  101.     // Start the threads.
  102.     mProducerThread->Resume();
  103.     mConsumerThread->Resume();
  104. }
  105.