Download Source Code

CThread Class Reference

Provides base thread functionality. More...

#include <Thread.h>

Inheritance diagram for CThread:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 CThread ()
 Default Constructor.
virtual ~CThread ()
 Destrucotr.
virtual BOOL Start ()
 Starts the thread.
virtual BOOL Stop ()
 Stops the thread.
virtual BOOL StartThread (LPVOID pData=NULL, BOOL bRestart=TRUE, BOOL bMessagePump=FALSE, BOOL bMfc=FALSE)
 Starts the thread.
virtual BOOL StopThread (BOOL bKill=TRUE, DWORD dwWait=3000)
 Stops any running thread.
BOOL IsRunning ()
 Returns non-zero if thread is currently running.
void PauseThread ()
 Pauses any running thread.
BOOL IsPausing ()
 Returns non-zero if thread is paused.
BOOL WaitPause (DWORD timeout=3000)
 Waits for thread to pause.
BOOL MsgWaitPause (DWORD timeout=3000)
 Pumps windows messages while waiting for thread to pause.
void UnpauseThread ()
 Unpauses thread.
BOOL IsStopping ()
 Returns non zero if thread is stopping.
virtual BOOL InitThread (LPVOID pData)
virtual BOOL DoThread (LPVOID pData)
virtual BOOL EndThread (LPVOID pData)
virtual DWORD GhostThread (LPVOID pData, DWORD dwThreadReturn)
void SetThreadPriority (DWORD dwPriority)
 Sets the thread priority.
DWORD GetThreadPriority ()
 Returns the thread priority.
BOOL ThreadSleep (DWORD add=0, DWORD abs=0)
BOOL GetExceptionStatus ()
 Returns the thread exception status.
HANDLE GetSafeThreadHandle ()
 Returns the thread handle.
BOOL WaitThreadInit (DWORD dwTimeout)
 Waits for the thread to initialize.
HANDLE GetStopEvent ()
 Returns the handle to the stop event.
DWORD GetThreadId ()
 Returns the thread ID.
void SignalStop ()
 Called to signal to the thread that it should exit.

Static Public Member Functions

static DWORD GetThreadCount ()
 Returns the total number of threads started using this class.
static DWORD GetRunningThreadCount ()
 Returns the total number of threads running using this class.
static void MessagePump (CThread *pThread=NULL)
 Windows message pump.
static BOOL WaitAllThreadInit (DWORD dwTimeout, DWORD dwPriority=15)
 Use to wait on all threads to initialize.

Protected Attributes

HANDLE m_hThread
 Handle to thead.
HANDLE m_hStop
 Stop event.
HANDLE m_hStopped
 Event triggered when thread has stopped.
HANDLE m_hInitialized
 Event triggered when thread has initialized.

Detailed Description

Provides base thread functionality.

Derive a class from from CThread and over-ride InitThread(), DoThread(), and EndThread() to provide custom functionality.


Member Function Documentation

virtual BOOL CThread::DoThread LPVOID  pData  )  [inline, virtual]
 

Parameters:
[in] pData - User defined value
Overide this function to provide custom thread work

This function is called over and over until it returns FALSE or StopThread() is called

pData - whatever was passed to StartThread() return TRUE if you want more processing (i.e. DoThread() will be called again ) return FALSE to end the thread

Returns:
Non-zero to continue thread, zero to terminate thread execution.
See also:
InitThread(), EndThread(), GhostThread()

Reimplemented in CAviEncode, CNetFile, CPipe, and CWinAsyncSocket.

virtual BOOL CThread::EndThread LPVOID  pData  )  [inline, virtual]
 

Parameters:
[in] pData - User defined value
Overide this function to provide custom cleanup

pData - whatever was passed to StartThread()

The return value is ignored if you want to return a thread value then set m_dwThreadReturn

Returns:
Return value is ignored
See also:
InitThread(), DoThread(), GhostThread()

Reimplemented in CAviEncode, CNetFile, CPipe, and CWinAsyncSocket.

BOOL CThread::GetExceptionStatus  )  [inline]
 

Returns the thread exception status.

This function returns non-zero if an exception has occured within the thread

static DWORD CThread::GetRunningThreadCount  )  [inline, static]
 

Returns the total number of threads running using this class.

This is retrieved from a static variable. So it only indicates threads running in the calling process.

Returns:
Number of threads running using this class
See also:

static DWORD CThread::GetThreadCount  )  [inline, static]
 

Returns the total number of threads started using this class.

This is retrieved from a static variable. So it only indicates threads started from the calling process.

Returns:
Number of threads started using this class
See also:

virtual DWORD CThread::GhostThread LPVOID  pData,
DWORD  dwThreadReturn
[inline, virtual]
 

Parameters:
[in] pData - User defined data
[in] dwThreadReturn - Thread return value
This function is called between when the thread tells the program it is done and when it actually exits

At this point the thread is unaccessable to the program and no class data should be accessed since it may now belong to another thread. Also this implementation is inline which means that no function call is actually made. The call is made from a static function so inline function code will not disapear when the class is deleted. If you overide this with a non-inline function it will be subject to destruction along with the class and you must make sure that the function code is still valid even after the thread has reported it has terminated.

The return value is the final value returned by the thread By default it is m_dwThreadReturn as it was after EndThread()

Just to make sure it is inline * any code in a class definition is inline

Returns:
Value returned by thread
See also:
InitThread(), DoThread(), EndThread()

virtual BOOL CThread::InitThread LPVOID  pData  )  [inline, virtual]
 

Parameters:
[in] pData - User defined data
The pData value passed to these functions represent the original value in m_pvoidData when the thread started. Changing m_pvoidData in these functions does not affect the passed value to the next function. This -hopefully- garentees that pData is always equal to the value passed to StartThread()

Overide this function to provide custom thread Initialization If you don't need initialization you can just use DoThread()

pData - whatever was passed to StartThread()

return TRUE if you want to continue the thread ( calling DoThread() ) return FALSE to end the thread now

Returns:
Non-zero to continue thread, zero to terminate thread execution.
See also:
DoThread(), EndThread(), GhostThread()

Reimplemented in CAviEncode, CNetFile, CPipe, and CWinAsyncSocket.

void CThread::MessagePump CThread pThread = NULL  )  [static]
 

Windows message pump.

Parameters:
[in] pThread - Thread for which messages will be pumped. Set to NULL to pump messages for all threads.

BOOL CThread::MsgWaitPause DWORD  timeout = 3000  )  [inline]
 

Pumps windows messages while waiting for thread to pause.

Parameters:
[in] timeout - Time in milli-seconds to wait for thread to pause.
Returns:
Non-zero if thread was paused before timeout
See also:

void CThread::PauseThread  )  [inline]
 

Pauses any running thread.

If the thread is started paused then InitThread() will still be called. The thread will pause before the first call to DoThread(). If StopThread() is called while the thread is paused, DoThread() will not* be called again. EndThread() and GhostThread() will still be called. In short, Pausing the thread prevents any calls to DoThread().

Returns:
See also:

void CThread::SetThreadPriority DWORD  dwPriority  )  [inline]
 

Sets the thread priority.

Parameters:
[in] dwPriority - Thread priority
Get/Set Thread Prioritys All Running CThreads share the same priority the threads will pause for m_dwPriority while processing Zero is the highest priority and will cause the thread to use all Idle CPU cycles

BOOL CThread::StartThread LPVOID  pData = NULL,
BOOL  bRestart = TRUE,
BOOL  bMessagePump = FALSE,
BOOL  bMfc = FALSE
[virtual]
 

Starts the thread.

Parameters:
[in] pData - Custom data passed on to thread
[in] bRestart - Non-zero to restart thread if running
[in] bMessagePump - Non-zero to have Windows messages pumped.
[in] bMfc - Non-zero for MFC friendly thread.
Returns:
Non-zero if thread was started.
See also:

BOOL CThread::StopThread BOOL  bKill = TRUE,
DWORD  dwWait = 3000
[virtual]
 

Stops any running thread.

Parameters:
[in] bKill - Set to non-zero to kill thread if it fails to stop gracefully.
[in] dwWait - Time in milli-seconds to wait for thread to stop before killing it.
Returns:
Non-zero if success
See also:

BOOL CThread::ThreadSleep DWORD  add = 0,
DWORD  abs = 0
[inline]
 

Parameters:
[in] add - Time to add to default wait time
[in] abs - Absolute wait time
Sleeps for amount of time specified by Priority Returns FALSE if the thread should terminate TRUE if it is ok to keep running

Returns:
Non-zero if thread is not stopping
See also:

static BOOL CThread::WaitAllThreadInit DWORD  dwTimeout,
DWORD  dwPriority = 15
[inline, static]
 

Use to wait on all threads to initialize.

Parameters:
[in] dwTimeout - Maximum amount of time in milli- seconds to wait for threads to initialize
[in] dwPriority - Time in milli-seconds between thread status checks.
This function will wait on all threads derived from this class that have been started to initialize. Cool trick sometimes...

Returns:
Non-zero if all threads started before timeout
See also:

BOOL CThread::WaitPause DWORD  timeout = 3000  )  [inline]
 

Waits for thread to pause.

Parameters:
[in] timeout - Time in milli-seconds to wait for thread to pause.
Returns:
Non-zero if thread was paused before timeout
See also:

BOOL CThread::WaitThreadInit DWORD  dwTimeout  ) 
 

Waits for the thread to initialize.

Parameters:
[in] dwTimeout - Maximum amount of time in milli- seconds to wait for thread to initialize.
Returns:
Non-zero if thread was initialized before timeout.
See also:


The documentation for this class was generated from the following files:
  • inc/Thread.h
  • Thread.cpp
Copyright Robert Umbehant
This documentation is covered by the LGPL