![]() |
Thread Class ReferenceThread base class.
More...
|
Public Member Functions | |
Thread (bool release=true) | |
virtual | ~Thread () |
virtual void | Run ()=0 |
pthread_t | GetThread () |
bool | IsRunning () |
void | SetRunning (bool x) |
bool | IsReleased () |
void | SetRelease (bool x) |
bool | DeleteOnExit () |
void | SetDeleteOnExit (bool x=true) |
bool | IsDestructor () |
void | Start () |
void | Stop () |
void | Wait () |
Static Public Member Functions | |
static threadfunc_t STDPREFIX | StartThread (threadparam_t) |
Protected Attributes | |
pthread_t | m_thread |
pthread_attr_t | m_attr |
Private Member Functions | |
Thread (const Thread &) | |
Thread & | operator= (const Thread &) |
Private Attributes | |
Semaphore | m_sem |
bool | m_running |
bool | m_release |
bool | m_b_delete_on_exit |
bool | m_b_destructor |
The Thread class is used by the resolver (ResolvServer) and running a detached socket (SocketThread). When you know some processing will take a long time and will freeze up a socket, there is always the possibility to call Detach() on that socket before starting the processing. When the OnDetached() callback is later called the processing can continue, now in its own thread.
Definition at line 68 of file Thread.h.
Thread::Thread | ( | bool | release = true |
) |
Definition at line 49 of file Thread.cpp.
References m_attr, m_release, m_sem, m_thread, Semaphore::Post(), SetRunning(), and StartThread().
00050 :m_thread(0) 00051 ,m_running(true) 00052 ,m_release(false) 00053 ,m_b_delete_on_exit(false) 00054 ,m_b_destructor(false) 00055 { 00056 #ifdef _WIN32 00057 // m_thread = ::CreateThread(NULL, 0, StartThread, this, 0, &m_dwThreadId); 00058 m_thread = (HANDLE)_beginthreadex(NULL, 0, &StartThread, this, 0, &m_dwThreadId); 00059 #else 00060 pthread_attr_init(&m_attr); 00061 pthread_attr_setdetachstate(&m_attr,PTHREAD_CREATE_DETACHED); 00062 if (pthread_create(&m_thread,&m_attr, StartThread,this) == -1) 00063 { 00064 perror("Thread: create failed"); 00065 SetRunning(false); 00066 } 00067 #endif 00068 m_release = release; 00069 if (release) 00070 m_sem.Post(); 00071 }
Thread::~Thread | ( | ) | [virtual] |
Definition at line 74 of file Thread.cpp.
References m_attr, m_b_destructor, m_running, m_thread, SetRelease(), SetRunning(), and Utility::Sleep().
00075 { 00076 m_b_destructor = true; 00077 if (m_running) 00078 { 00079 SetRelease(true); 00080 SetRunning(false); 00081 /* 00082 Sleep one second to give thread class Run method enough time to 00083 release from run loop 00084 */ 00085 Utility::Sleep(1000); 00086 } 00087 #ifdef _WIN32 00088 if (m_thread) 00089 ::CloseHandle(m_thread); 00090 #else 00091 pthread_attr_destroy(&m_attr); 00092 #endif 00093 }
Thread::Thread | ( | const Thread & | ) | [inline, private] |
threadfunc_t STDPREFIX Thread::StartThread | ( | threadparam_t | zz | ) | [static] |
Definition at line 96 of file Thread.cpp.
References DeleteOnExit(), IsDestructor(), m_running, Run(), SetRunning(), Utility::Sleep(), and Wait().
Referenced by Thread().
00097 { 00098 /* 00099 Sleep here to wait for derived thread class constructor to setup 00100 vtable... hurts just looking at it 00101 */ 00102 Utility::Sleep(5); 00103 00104 Thread *p = (Thread *)zz; 00105 00106 p -> Wait(); 00107 if (p -> m_running) 00108 { 00109 p -> Run(); 00110 } 00111 p -> SetRunning(false); // if return 00112 if (p -> DeleteOnExit() && !p -> IsDestructor()) 00113 { 00114 delete p; 00115 } 00116 #ifdef _WIN32 00117 _endthreadex(0); 00118 #endif 00119 return (threadfunc_t)NULL; 00120 }
virtual void Thread::Run | ( | ) | [pure virtual] |
pthread_t Thread::GetThread | ( | ) | [inline] |
bool Thread::IsRunning | ( | ) |
Definition at line 123 of file Thread.cpp.
References m_running.
Referenced by SocketThread::Run(), SocketHandlerThread::Run(), ResolvServer::Run(), and SocketThread::~SocketThread().
00124 { 00125 return m_running; 00126 }
void Thread::SetRunning | ( | bool | x | ) |
Definition at line 129 of file Thread.cpp.
References m_running.
Referenced by ResolvServer::Run(), StartThread(), Thread(), SocketThread::~SocketThread(), and ~Thread().
00130 { 00131 m_running = x; 00132 }
bool Thread::IsReleased | ( | ) |
Definition at line 135 of file Thread.cpp.
References m_release.
00136 { 00137 return m_release; 00138 }
void Thread::SetRelease | ( | bool | x | ) |
Definition at line 141 of file Thread.cpp.
References m_release, m_sem, and Semaphore::Post().
Referenced by SocketThread::~SocketThread(), and ~Thread().
bool Thread::DeleteOnExit | ( | ) |
Definition at line 149 of file Thread.cpp.
References m_b_delete_on_exit.
Referenced by StartThread().
00150 { 00151 return m_b_delete_on_exit; 00152 }
void Thread::SetDeleteOnExit | ( | bool | x = true |
) |
Definition at line 155 of file Thread.cpp.
References m_b_delete_on_exit.
Referenced by SocketThread::Run().
00156 { 00157 m_b_delete_on_exit = x; 00158 }
bool Thread::IsDestructor | ( | ) |
Definition at line 161 of file Thread.cpp.
References m_b_destructor.
Referenced by StartThread().
00162 { 00163 return m_b_destructor; 00164 }
void Thread::Start | ( | ) | [inline] |
void Thread::Stop | ( | ) | [inline] |
void Thread::Wait | ( | ) |
Reimplemented in SocketHandlerThread.
Definition at line 167 of file Thread.cpp.
References m_sem, and Semaphore::Wait().
Referenced by StartThread().
pthread_t Thread::m_thread [protected] |
pthread_attr_t Thread::m_attr [protected] |
Semaphore Thread::m_sem [private] |
Reimplemented in SocketHandlerThread.
Definition at line 116 of file Thread.h.
Referenced by SetRelease(), Thread(), and Wait().
bool Thread::m_running [private] |
Definition at line 117 of file Thread.h.
Referenced by IsRunning(), SetRunning(), StartThread(), and ~Thread().
bool Thread::m_release [private] |
bool Thread::m_b_delete_on_exit [private] |
bool Thread::m_b_destructor [private] |