00001 /******************************************************************** 00002 Copyright (c) 2001, Lee Patterson & Ant Works Software 00003 http://ssobjects.sourceforge.net 00004 00005 Original source from Win32 Multithreaded Programming 00006 Copyright (c) 1997 by Aaron Michael Cohen and Mike Woodring 00007 00008 filename : mclcritsec.cpp 00009 author : Lee Patterson (lee@antws.com) 00010 *********************************************************************/ 00011 00012 #ifndef MCLTHREAD_H 00013 #define MCLTHREAD_H 00014 00015 #include "msdefs.h" 00016 00017 #include <pthread.h> 00018 00019 namespace ssobjects 00020 { 00021 00022 typedef void* threadReturn; 00023 00024 00025 // forward declaration for CMclThreadHandler... 00026 class CMclThread; 00027 00028 // CMclThreadHandler encapsulates the thread procedure for a 00029 // CMclThread object, each instantiation of a thread handler can 00030 // be used by multiple threads at a time but the base CMclThreadHandler 00031 // class does NOT provide any internal synchronization for multiple threads 00032 // using a single instance simultaneously. A derived class could provide 00033 // this internal synchronization however... 00034 class CMclThreadHandler 00035 { 00036 public: 00037 // destructor does nothing, it is simply a placeholder for 00038 // ensure the destructors of derived classes are virtual... 00039 virtual ~CMclThreadHandler(); 00040 00041 // This is a pure virtual function with no implementation 00042 // it must be implemented in a derived class. 00043 // The "this" object 00044 // inside ThreadHandlerProc() will be the CMclThreadHandler derived 00045 // object itself. 00046 // The procedure should return the exit code of the thread when finished... 00047 virtual threadReturn ThreadHandlerProc(void) = 0; 00048 }; 00049 00050 class CMclThread 00051 { 00052 protected: 00053 CMclThreadHandler *m_pcThreadHandler; 00054 pthread_t m_Thread; 00055 DWORD m_dwStatus; 00056 00057 public: 00058 00059 // only the thread handler reference needs to 00060 // be supplied since the other arguments have default values... 00061 CMclThread( CMclThreadHandler *pcThreadHandler); 00062 00063 void Wait(DWORD dwMilliSeconds=INFINITE); 00064 00065 //return the pthread 00066 pthread_t GetThread() {return m_Thread;} 00067 00068 // suspend the thread... 00069 DWORD Suspend(void); 00070 00071 // resume the thread... 00072 DWORD Resume(void); 00073 00074 // terminate the thread... 00075 BOOL Terminate( DWORD dwExitCode); 00076 00077 // read a thread's exit code... 00078 BOOL GetExitCode( DWORD *pdwExitCode); 00079 00080 // set a thread's priority... 00081 BOOL SetPriority( int nPriority); 00082 00083 // read a thread's priority... 00084 int GetPriority(void); 00085 00086 // get the internal thread id... 00087 DWORD GetThreadId(void); 00088 00089 private: 00090 // this is a static function used to kick-start the thread handler... 00091 static void* CallThreadHandlerProc(void *pThreadHandler); 00092 CMclThread(const CMclThread&); 00093 CMclThread& operator=(const CMclThread&); 00094 }; 00095 00096 }; // namespace 00097 00098 #endif //MCLTHREAD_H