Logo
~Sockets~
~Examples~
~Contact~


Thread Class Reference
[Threading]

Thread base class. More...

#include <Thread.h>

Inheritance diagram for Thread:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 Thread (bool release=true)
virtual ~Thread ()
virtual void Run ()=0
bool IsRunning ()
void SetRunning (bool x)
bool IsReleased ()
void SetRelease (bool x)
bool DeleteOnExit ()
void SetDeleteOnExit (bool x=true)
bool IsDestructor ()

Static Public Member Functions

static threadfunc_t STDPREFIX StartThread (threadparam_t)

Private Member Functions

 Thread (const Thread &)
Threadoperator= (const Thread &)

Private Attributes

pthread_t m_thread
bool m_running
bool m_release
bool m_b_delete_on_exit
bool m_b_destructor

Detailed Description

Thread base class.

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 61 of file Thread.h.


Constructor & Destructor Documentation

Thread::Thread ( bool  release = true  ) 

Definition at line 46 of file Thread.cpp.

References m_release, m_thread, SetRunning(), and StartThread().

00047 :m_thread(0)
00048 ,m_running(true)
00049 ,m_release(false)
00050 ,m_b_delete_on_exit(false)
00051 ,m_b_destructor(false)
00052 {
00053 #ifdef _WIN32
00054 //      m_thread = ::CreateThread(NULL, 0, StartThread, this, 0, &m_dwThreadId);
00055         m_thread = (HANDLE)_beginthreadex(NULL, 0, &StartThread, this, 0, &m_dwThreadId);
00056 #else
00057         pthread_attr_t attr;
00058 
00059         pthread_attr_init(&attr);
00060         pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
00061         if (pthread_create(&m_thread,&attr, StartThread,this) == -1)
00062         {
00063                 perror("Thread: create failed");
00064                 SetRunning(false);
00065         }
00066 //      pthread_attr_destroy(&attr);
00067 #endif
00068         m_release = release;
00069 }

Thread::~Thread (  )  [virtual]

Definition at line 72 of file Thread.cpp.

References m_b_destructor, m_running, m_thread, SetRelease(), and SetRunning().

00073 {
00074         m_b_destructor = true;
00075         if (m_running)
00076         {
00077                 SetRelease(true);
00078                 SetRunning(false);
00079 #ifdef _WIN32
00080                 Sleep(1000);
00081 #else
00082                 sleep(1);
00083 #endif
00084         }
00085 #ifdef _WIN32
00086         if (m_thread)
00087                 ::CloseHandle(m_thread);
00088 #endif
00089 }

Thread::Thread ( const Thread  )  [inline, private]

Definition at line 80 of file Thread.h.

00080 {}


Member Function Documentation

threadfunc_t STDPREFIX Thread::StartThread ( threadparam_t   )  [static]

Definition at line 92 of file Thread.cpp.

References DeleteOnExit(), IsDestructor(), m_release, m_running, Run(), and SetRunning().

Referenced by Thread().

00093 {
00094         Thread *p = (Thread *)zz;
00095 
00096         while (p -> m_running && !p -> m_release)
00097         {
00098 #ifdef _WIN32
00099                 Sleep(1000);
00100 #else
00101                 sleep(1);
00102 #endif
00103         }
00104         if (p -> m_running)
00105         {
00106                 p -> Run();
00107         }
00108         p -> SetRunning(false); // if return
00109         if (p -> DeleteOnExit() && !p -> IsDestructor())
00110         {
00111                 delete p;
00112         }
00113 #ifdef _WIN32
00114         _endthreadex(0);
00115 #endif
00116         return (threadfunc_t)NULL;
00117 }

virtual void Thread::Run (  )  [pure virtual]

Implemented in ResolvServer, and Socket::SocketThread.

Referenced by StartThread().

bool Thread::IsRunning (  ) 

Definition at line 120 of file Thread.cpp.

References m_running.

Referenced by Socket::SocketThread::Run(), ResolvServer::Run(), and Socket::SocketThread::~SocketThread().

00121 {
00122         return m_running;
00123 }

void Thread::SetRunning ( bool  x  ) 

Definition at line 126 of file Thread.cpp.

References m_running.

Referenced by ResolvServer::Run(), StartThread(), Thread(), Socket::SocketThread::~SocketThread(), and ~Thread().

00127 {
00128         m_running = x;
00129 }

bool Thread::IsReleased (  ) 

Definition at line 132 of file Thread.cpp.

References m_release.

00133 {
00134         return m_release;
00135 }

void Thread::SetRelease ( bool  x  ) 

Definition at line 138 of file Thread.cpp.

References m_release.

Referenced by Socket::SocketThread::~SocketThread(), and ~Thread().

00139 {
00140         m_release = x;
00141 }

bool Thread::DeleteOnExit (  ) 

Definition at line 144 of file Thread.cpp.

References m_b_delete_on_exit.

Referenced by StartThread().

00145 {
00146         return m_b_delete_on_exit;
00147 }

void Thread::SetDeleteOnExit ( bool  x = true  ) 

Definition at line 150 of file Thread.cpp.

References m_b_delete_on_exit.

Referenced by Socket::SocketThread::Run().

00151 {
00152         m_b_delete_on_exit = x;
00153 }

bool Thread::IsDestructor (  ) 

Definition at line 156 of file Thread.cpp.

References m_b_destructor.

Referenced by StartThread().

00157 {
00158         return m_b_destructor;
00159 }

Thread& Thread::operator= ( const Thread  )  [inline, private]

Definition at line 81 of file Thread.h.

00081 { return *this; }


Member Data Documentation

pthread_t Thread::m_thread [private]

Definition at line 86 of file Thread.h.

Referenced by Thread(), and ~Thread().

bool Thread::m_running [private]

Definition at line 88 of file Thread.h.

Referenced by IsRunning(), SetRunning(), StartThread(), and ~Thread().

bool Thread::m_release [private]

Definition at line 89 of file Thread.h.

Referenced by IsReleased(), SetRelease(), StartThread(), and Thread().

Definition at line 90 of file Thread.h.

Referenced by DeleteOnExit(), and SetDeleteOnExit().

bool Thread::m_b_destructor [private]

Definition at line 91 of file Thread.h.

Referenced by IsDestructor(), and ~Thread().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4