SmtpThread::Thread Class Reference

#include <Thread.h>

Inherited by SmtpThread::ThreadedSMTPConnection.

Inheritance diagram for SmtpThread::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'.


Constructor & Destructor Documentation

SmtpThread::Thread::Thread (  ) 

Constructor.

SmtpThread::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... ;)


Member Function Documentation

int SmtpThread::Thread::Start ( void *  arg  ) 

Start threading.

Parameters:
arg pointer to arguments.
Returns:
exit-code of 'pthread_create'

pthread_t SmtpThread::Thread::getThreadId (  ) 

Returns the thread-id.

Returns:
thread-id

bool SmtpThread::Thread::isRunning (  ) 

Checks, whether or not the thread is running.

Returns:
true / false = running / not running

void SmtpThread::Thread::Stop (  ) 

Stop threading.

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

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

Parameters:
arg pointer to the arguments.

void * SmtpThread::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:
arg pointer to the arguments.

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

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

void SmtpThread::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:
arg pointer to the arguments.

Reimplemented in SmtpThread::ThreadedSMTPConnection.

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

Helper to initialize the parameters provided to Start.

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

Initiallizes the parameters passed to "Start".

Parameters:
a pointer to the arguments.

void SmtpThread::Thread::getLock (  )  [protected]

Locks the mutex "_mutex".

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

See also:
Thread::releaseLock

void SmtpThread::Thread::releaseLock (  )  [protected]

Unlocks the mutex "_mutex".

See also:
Thread::getLock


Member Data Documentation

bool SmtpThread::Thread::_isRunning [protected]

Wether or not the thread is running.

pthread_mutex_t SmtpThread::Thread::_mutex [protected]

Mutex.


Generated on Sun Dec 2 13:20:07 2007 for libSmtpThread by  doxygen 1.5.1