00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _SOCKETS_Thread_H
00033 #define _SOCKETS_Thread_H
00034
00035 #include "sockets-config.h"
00036 #ifdef _WIN32
00037 #else
00038 #include <pthread.h>
00039 #endif
00040 #include "Semaphore.h"
00041
00042 #ifdef SOCKETS_NAMESPACE
00043 namespace SOCKETS_NAMESPACE {
00044 #endif
00045
00046 #ifdef _WIN32
00047
00048
00049
00050
00051 typedef unsigned threadfunc_t;
00052 typedef void * threadparam_t;
00053 #define STDPREFIX __stdcall
00054 #else
00055
00056 typedef void * threadfunc_t;
00057 typedef void * threadparam_t;
00058 #define STDPREFIX
00059 #endif
00060
00068 class Thread
00069 {
00070 public:
00071 Thread(bool release = true);
00072 virtual ~Thread();
00073
00074 static threadfunc_t STDPREFIX StartThread(threadparam_t);
00075
00076 virtual void Run() = 0;
00077
00078 #ifdef _WIN32
00079 HANDLE GetThread() { return m_thread; }
00080 unsigned GetThreadId() { return m_dwThreadId; }
00081 #else
00082 pthread_t GetThread() { return m_thread; }
00083 #endif
00084
00085 bool IsRunning();
00086 void SetRunning(bool x);
00087 bool IsReleased();
00088 void SetRelease(bool x);
00089 bool DeleteOnExit();
00090 void SetDeleteOnExit(bool x = true);
00091 bool IsDestructor();
00092
00093 void Start() {
00094 SetRelease(true);
00095 }
00096
00097 void Stop() {
00098 Start();
00099 SetRunning(false);
00100 }
00101
00102 void Wait();
00103
00104 protected:
00105 #ifdef _WIN32
00106 HANDLE m_thread;
00107 unsigned m_dwThreadId;
00108 #else
00109 pthread_t m_thread;
00110 pthread_attr_t m_attr;
00111 #endif
00112
00113 private:
00114 Thread(const Thread& ) {}
00115 Thread& operator=(const Thread& ) { return *this; }
00116 Semaphore m_sem;
00117 bool m_running;
00118 bool m_release;
00119 bool m_b_delete_on_exit;
00120 bool m_b_destructor;
00121 };
00122
00123
00124 #ifdef SOCKETS_NAMESPACE
00125 }
00126 #endif
00127
00128 #endif // _SOCKETS_Thread_H
00129