Thread Class Reference

#include <Thread.h>

Inherited by ThreadedSMTPConnection.

Inheritance diagram for Thread:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 Thread ()
 ~Thread ()
int Start (void *arg)
pthread_t getThreadId ()
bool isRunning ()
void Stop ()

Protected Member Functions

int Run (void *arg)
virtual void Setup ()
virtual void Execute (void *)
void * Arg () const
void Arg (void *a)
void getLock ()
void releaseLock ()

Static Protected Member Functions

static void * EntryPoint (void *)

Protected Attributes

bool _isRunning
pthread_mutex_t _mutex

Detailed Description

Thread-Class.

This abstract class wraps a class around a threading function.

Threading is startet through calling 'Start', which sets the protected member-variable '_isRunning'. The state of the thread can be checked with the member-function 'isRunning'.

You must overwrite the method 'Execute' to do something usefull within the thread.

'Execute' should be constructed as an endless loop like:

void InheritedClassname::Execute( void * arg ) { while( _isRunning ) { // do something... } }

When 'Start' is called, it starts the thread and executes 'Setup' and 'Execute'.

To stop threading, simply call 'Stop'.

Definition at line 42 of file Thread.h.


Constructor & Destructor Documentation

Thread::Thread (  ) 

Constructor.

Definition at line 15 of file Thread.cc.

References _isRunning, and _mutex.

Thread::~Thread (  ) 

Destructor.

NOTE: Under normal conditions I would use a purely virtual destructor. But unfortunately most people (me too) often forget to call the 'Stop' member-function in the destructor of the derived classes.

So calling delete on an object, which still has a running thread, may not behave well, especially when the destructor removes members of the object, while the thread is trying to access them.

So, I implemented a non-virtual destructor, which stops threading, when the thread is still running. This will cause most compilers to issue a warning, but it seems to be the easiest way, to get your code working... ;)

Definition at line 26 of file Thread.cc.

References _isRunning, and Stop().

Here is the call graph for this function:


Member Function Documentation

int Thread::Start ( void *  arg  ) 

Start threading.

Parameters:
void* arg typeless argument.
Returns:
int exit-code of 'pthread_create'

Definition at line 40 of file Thread.cc.

References Arg(), and EntryPoint().

Here is the call graph for this function:

pthread_t Thread::getThreadId (  ) 

Returns the thread-id.

Returns:
pthread_t thread-id

Definition at line 130 of file Thread.cc.

bool Thread::isRunning (  ) 

Checks, whether or not the thread is running.

Returns:
bool true / false = running / not running

Definition at line 141 of file Thread.cc.

References _isRunning.

void Thread::Stop (  ) 

Stop threading.

Definition at line 151 of file Thread.cc.

References _isRunning.

Referenced by ~Thread().

int Thread::Run ( void *  arg  )  [protected]

Gets called immediatly after "EntryPoint". Calls "Setup" and "Execute".

Parameters:
void* arg typeless argument.

Definition at line 66 of file Thread.cc.

References Execute(), and Setup().

Referenced by EntryPoint().

Here is the call graph for this function:

void * Thread::EntryPoint ( void *   )  [static, protected]

This is the entrypoint for threading. This function must be declared as "static", because the pthread-library expects a normal C-type-function as an entrypoint.

Parameters are passed as pointer to void and are the same as passed to "Start".

Parameters:
void* arg typeless argument.

Definition at line 80 of file Thread.cc.

References Arg(), and Run().

Referenced by Start().

Here is the call graph for this function:

void Thread::Setup (  )  [protected, virtual]

Initializes some thread-variables and sets up a mutex. The mutex can be accessed through the member-varaible "_mutex".

Definition at line 104 of file Thread.cc.

References _isRunning, and _mutex.

Referenced by Run().

void Thread::Execute ( void *   )  [protected, virtual]

The main threading-functionality goes here. You should create an endless loop like:

while( _isRunning) { // do something usefull... }

Parameters are passed as pointer to void and are the same as passed to "Start".

Parameters:
void* arg typeless argument.

Reimplemented in ThreadedSMTPConnection.

Definition at line 119 of file Thread.cc.

Referenced by Run().

void* Thread::Arg (  )  const [inline, protected]

Helper to initialize the parameters provided to "Start".

Definition at line 146 of file Thread.h.

Referenced by EntryPoint(), and Start().

void Thread::Arg ( void *  a  )  [inline, protected]

Initiallizes the parameters passed to "Start".

Parameters:
void* a typeless argument.

Definition at line 156 of file Thread.h.

void Thread::getLock (  )  [protected]

Locks the mutex "_mutex".

NOTE: Use with care, cause it might get you a deadlock!

See also:
Thread::releaseLock

Definition at line 167 of file Thread.cc.

References _mutex.

Referenced by ThreadedSMTPConnection::Execute(), ThreadedSMTPConnection::sendMail(), ThreadedSMTPConnection::setMyHostname(), ThreadedSMTPConnection::setSMTPHost(), and ThreadedSMTPConnection::setSMTPPort().

void Thread::releaseLock (  )  [protected]

Unlocks the mutex "_mutex".

See also:
Thread::getLock

Definition at line 178 of file Thread.cc.

References _mutex.

Referenced by ThreadedSMTPConnection::Execute(), ThreadedSMTPConnection::sendMail(), ThreadedSMTPConnection::setMyHostname(), ThreadedSMTPConnection::setSMTPHost(), and ThreadedSMTPConnection::setSMTPPort().


Member Data Documentation

bool Thread::_isRunning [protected]

Wether or not the thread is running.

Definition at line 162 of file Thread.h.

Referenced by ThreadedSMTPConnection::Execute(), isRunning(), Setup(), Stop(), Thread(), and ~Thread().

pthread_mutex_t Thread::_mutex [protected]

Mutex.

Definition at line 165 of file Thread.h.

Referenced by getLock(), releaseLock(), Setup(), and Thread().


The documentation for this class was generated from the following files:
Generated on Sat Oct 20 15:48:50 2007 for libSmtpThread by  doxygen 1.5.1