Thread.h
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:2k
- //File Name : Thread.h.
- //Standard windows thread common header file that writen by Devia.
- //This code is free all, you can distribute it.
- //Author : Devia Li(devia@sina.com)
- //DateTime : 2004-04-04
- #ifndef _THREAD_H
- #define _THREAD_H
- #include "mutex.h"
- //thread state enum type.
- typedef enum {TS_STOPED = 0, TS_RUNNING = 1, TS_SUSPEDED = 2} THREAD_STATE;
- //////////////////////////////////////////////////////////////////////////
- class NETLIBDLLEXPORT CThread
- {
- public:
- //constructor and destructor
- CThread();
- virtual ~CThread();
- //when destructor, terminate the thread flag
- bool m_bFreeAndTerminate;
- //create the thread
- bool Create(bool bCreateAndSuspended = false, LPVOID lpParam = NULL);
- //destroy the thread
- virtual void Destroy();
- //Start
- void Start();
- //Stop
- void Stop();
- //Pause
- void Pause();
- //Resume
- void Resume();
-
- //get thread id
- DWORD getThreadId() const {
- return m_dwThreadId;
- };
- //get thread handle
- HANDLE getSafeHandle() const {
- return m_hThread;
- }
- //get the thread state.
- THREAD_STATE getThreadState() const {
- return this->m_tsState;
- }
- //get the thread terminate flag.
- bool CanBeTerminate() const {
- return this->m_bCanTerminate;
- }
- //get the thread param value.
- LPVOID getThreadParams() const{
- return this->m_lpParams;
- }
-
- protected:
- //create the thread
- static ulong WINAPI fnThreadProc(LPVOID lpParameter);
- //overrides execute function.
- virtual void Execute() = 0;
-
- //start, stop, pause and resume event process functions.
- virtual void OnStart(){ /* empty */ }
- virtual void OnStop(){ /* empty */ }
- virtual void OnSuspend(){ /* empty */ }
- virtual void OnResume(){ /* empty */ }
- private:
- typedef enum { TE_START = 0, TE_STOP = 1, TE_SUSPEND = 2, TE_RESUME = 3 } THREAD_EVENT;
- void DoEvent(CThread::THREAD_EVENT te);
- protected:
- THREAD_STATE m_tsState; //thread state variable.
- bool m_bCanTerminate; //terminate thread flag.
- CMutex m_csCritSect; //cirtical section object.
- private:
- LPVOID m_lpParams; //user's parameter
- HANDLE m_hThread; //thread handle
- ulong m_dwThreadId; //thread id
- };
- #endif //_THREAD_H