WorkerThread.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:4k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   WorkerThread.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Abstract base class, defining methods to launch and 
  11. *                       stop a worker thread. All worker threads in our
  12. *                       application derive from this class.
  13. *                                                                             
  14. *   Authors: Eran Yariv - 28484475                                           
  15. *            Moshe Zur  - 24070856                                           
  16. *                                                                            
  17. *                                                                            
  18. *   Date: 23/09/98                                                           
  19. *                                                                            
  20. ******************************************************************************/
  21. #include <stdafx.h>
  22. #include "WorkerThread.h"
  23. /*------------------------------------------------------------------------------
  24.   Function: StartWorkerThread
  25.   Purpose:  Starts the main worker function - ThreadEntry. We make sure only a
  26.             single instance of the thread is running.
  27.   Input:    Pointer to the thread's params.
  28.   Output:   None.
  29.   Remarks:  Since we want ThreadEntry to be a virtual function, we can't start
  30.             the thread from this function. Instead we call the static function
  31.             InvokeThreadEntry, passing to it the pointer to our class. This way
  32.             we can invoke the dynamically binded thread entry, and pass the 
  33.             params (as class private members).
  34. ------------------------------------------------------------------------------*/
  35. void 
  36. CWorkerThread::StartWorkerThread (LPVOID pvParam)
  37. {
  38.     if (m_bThreadIsRunning) // Only one worker thread is handled at a time !
  39.         return;
  40.     m_bEndThread = FALSE;   // This flag is checked by thread, making it TRUE ends thread.
  41.     m_pvParam = pvParam;
  42.     CWinThread* pThread = AfxBeginThread( InvokeThreadEntry, this );
  43.     if (pThread) 
  44.     {
  45.         m_hThread = pThread -> m_hThread;
  46.         m_bThreadIsRunning = TRUE;
  47.     }
  48. }
  49. /*------------------------------------------------------------------------------
  50.   Function: EndWorkerThread
  51.   Purpose:  Stops the thread, by changing the appropiate flag.
  52.   Input:    bWaitForTermination : If flag is on we wait on the thread terminate
  53.             event.
  54.   Output:   None.
  55.   Remarks:  In some cases, the thread main loop be blocked for a long time
  56.             before reaching the flag. In this cases the derived class may 
  57.             override this method in order to use their own signal to terminate.
  58. ------------------------------------------------------------------------------*/
  59. void 
  60. CWorkerThread::EndWorkerThread (BOOL bWaitForTermination)
  61. {
  62.     if (m_bThreadIsRunning)
  63.     {
  64.         m_bEndThread = TRUE;    // Notify thread to die
  65.         if (bWaitForTermination)
  66.             WaitForSingleObject ( m_hThread, INFINITE );
  67.         m_bThreadIsRunning = FALSE;
  68.     }
  69. }
  70. /*------------------------------------------------------------------------------
  71.   Function: InvokeThreadEntry
  72.   Purpose:  Static function passed to AfxBeginThread. It's purpose is to invoke
  73.             the working thread main loop - ThreadEntry.
  74.   Input:    pvParam : Pointer to the derived class.
  75.   Output:   None.
  76.   Remarks:  See explanation at StartWorkerThread.
  77. ------------------------------------------------------------------------------*/
  78. UINT 
  79. CWorkerThread::InvokeThreadEntry (LPVOID pvParam)
  80. {
  81.     CWorkerThread *pWorkerThread = (CWorkerThread*)pvParam;
  82.     return pWorkerThread ? pWorkerThread -> ThreadEntry (pWorkerThread -> m_pvParam)
  83.         : 0;
  84. }