www.pudn.com > ChatUseIOCP.rar > DtThread.h
#pragma once #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0400 #endif #ifndef _WINBASE_ #include#endif #ifndef DTLOCKER #include "DTLOCKER.h" #ifndef DTLOCKER #error DtThread: CDtLocker is required! #endif #endif #define DTTHREAD ////////////////////////////////////////////////////////////////////////// /// /// /// @class DtThread - A Thread wrapper class /// @version 1.0 /// @author Jonas Gauffin /// @homepage http://www.gauffin.org/cpp /// /// @brief A thread baseclass, takes care of all thread handling, /// just overload ThreadFunc and your ready and set. /// /// @since 2003-04-23 jg File created /// @since 2003-07-30 jg Some things is rewritten /// /// ////////////////////////////////////////////////////////////////////////// namespace Datatal { class DtThread { public: DtThread(void); ~DtThread(void); public: /// Start the thread. /// @returns 0 = Ok, 1 = Thread already running, 2 = Failed to create & start thread. DWORD StartThread(); /// Stops the thread by signalling m_hStopEvent. If it doesnt work, it uses KillThread /// @returns 0 = Ok, 1 = thread is not running, 2 = thread was not stopped. DWORD StopThread(); virtual void OnThreadLogWrite(int nPriority, const char* pszLogEntry) {}; /// Function that will be called when the thread is running. /// You _MUST_ wait for the m_hStopEvent event if you want the StopThread() method to work correctly. /// @see StopThread virtual void ThreadFunc(HANDLE hStopEvent) = 0; /// Returns true if the thread is running. bool IsThreadRunning() const { return m_bThreadRunning; }; /// Locks the thread, by using the threads critical section, should be used when reading critical variables. /// @see UnlockThread() __inline void LockThread() { m_CritThread.Lock(); }; /// Unlocks the threads critical section, MUST BE CALLED after LockThread /// @see LockThread() __inline void UnlockThread() { m_CritThread.Unlock(); }; /// Will get called when the thread has been stopped. virtual void OnThreadStopped() {}; /// Will get called when the thread is running. virtual void OnThreadRunning() {}; int GetThreadId() const { return m_nThreadId; }; HANDLE GetThreadHandle() const { return m_hThread; }; protected: //Our thread that do all the actual work static unsigned __stdcall InternalThreadFunc(void* pClient); private: DtCriticalSection m_CritThread; /// Lock when we are using the thread control vars bool m_bThreadRunning; /// True when the thread is running HANDLE m_hThread; int m_nThreadId; protected: HANDLE m_hStopEvent; }; };//Namespace