#include <Thread.h>
Inherited by SmtpThread::ThreadedSMTPConnection.
Inheritance diagram for SmtpThread::Thread:
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 |
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'.
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... ;)
int SmtpThread::Thread::Start | ( | void * | arg | ) |
Start threading.
arg | pointer to arguments. |
pthread_t SmtpThread::Thread::getThreadId | ( | ) |
Returns the thread-id.
bool SmtpThread::Thread::isRunning | ( | ) |
Checks, whether or not the thread is running.
void SmtpThread::Thread::Stop | ( | ) |
Stop threading.
int SmtpThread::Thread::Run | ( | void * | arg | ) | [protected] |
Gets called immediatly after "EntryPoint". Calls "Setup" and "Execute".
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".
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".
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".
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!
void SmtpThread::Thread::releaseLock | ( | ) | [protected] |
bool SmtpThread::Thread::_isRunning [protected] |
Wether or not the thread is running.
pthread_mutex_t SmtpThread::Thread::_mutex [protected] |
Mutex.