#include <mcs.hh>
This class let you run code in a separate thread. You can derive this class and overload its virtual method run(), this will be the thread body being executed when you start the new thread.
A thread can be started from the parent thread in two ways:
The child thread will always execute three methods: initial(), run(), final(). These are virtual methods, so you can overload them in derived classes. The initial() method can be used to perform initialization tasks. It is useful because it is guarranteed that the start() (or startDetached()) method won't return until the initial() method has returned, so the main thread knows that the separate thread is already running. The run() method is the body of the separate thread execution. A thread can terminate in three different ways:
The final() method can be used to free resources allocated by the separate thread. This method is useful because a thread can terminate either by returning from the run() method, or because it is stopped from another thread. In any case the final() method will be executed in the separate thread, so it can be used to free allocated resources.
If the adress of a parent object has been provided in the constructor call, then it will be notified of thread termination (see notify() method), after the final() method has been executed.
This class provide the state() method which tells in which "state" of its life-cycle a thread is. The value of the state can be one of the follows:
Definition at line 2508 of file mcs.hh.
Public Member Functions | |
Event * | error () |
Return last error message. | |
int | id () |
Returns the Thread object identificator. | |
Thread & | operator= (const Thread &) |
Declared to avoid using of default assignment operator. | |
Thread * | parent () |
Returns the address of the parent. | |
void | start () |
Start a new thread in the joinable state. | |
void | startDetached (bool selfDelete=false) |
Start a new thread in the detached state. | |
int | state () |
Return the state of the thread. | |
void | stop () |
Stop thread execution. | |
Thread (int id=0, Thread *parent=NULL) | |
Constructor. | |
Thread (const Thread &) | |
Declared to avoid using of default copy constructor. | |
virtual | ~Thread () |
Destructor. | |
Protected Member Functions | |
virtual void | final () |
Finalization method. | |
virtual void | initial () |
Initialization method. | |
virtual void | notify (int id, Thread *ref) |
A method called from child threads to notify their termination. | |
virtual void | run () |
Body of the thread execution. | |
void | set_cancel_state (bool cancel) |
Set cancellation state for current thread. | |
void | test_cancel () |
Test if a cancellation request is pending. | |
Protected Attributes | |
Event * | lerror |
Last error. | |
Private Member Functions | |
bool | checkTerminating () |
Check if the thread is already terminating (true) or not (false). | |
Static Private Member Functions | |
static void | cleanup_Handler (void *This) |
Static wrapper around final(). | |
static void * | RunThread (void *args) |
Starting point for the separate thread. | |
Private Attributes | |
bool | detached |
Tell if the thread is to be created in the detached state. | |
int | lid |
General purpose Thread identifier. | |
Thread * | lparent |
Reference to parent Thread object, if any is given in the constructor. | |
int | lstate |
State of the object, see state() method. | |
pthread_t | lthrID |
System's thread identifier. | |
MCS_DEBUG_ALLOC | |
bool | selfDelete |
Tell if the object should delete himself once the thread is terminated. | |
Synchro | syn_lstate |
mcs::Thread::Thread | ( | const Thread & | ) |
Declared to avoid using of default copy constructor.
mcs::Thread::Thread | ( | int | id = 0 , |
|
Thread * | parent = NULL | |||
) |
mcs::Thread::~Thread | ( | ) | [virtual] |
Destructor.
You should avoid destroying Thread objects unless you are sure that the separate thread has terminated.
A good choice to destroy Thread objects is to place a "delete" statement inside the notify() method, otherwise you can start the thread in the detached state (with the startDetached() method) and set the "selfDelete" flag to true.
bool mcs::Thread::checkTerminating | ( | ) | [private] |
void mcs::Thread::cleanup_Handler | ( | void * | This | ) | [static, private] |
void mcs::Thread::final | ( | ) | [protected, virtual] |
Finalization method.
This method is executed inside the separate thread once the thread has terminated (the run() method has returned or another thread called the stop() method).
You can use this method to perform finalizations like memory cleanup, objects destructions, etc...
int mcs::Thread::id | ( | ) |
void mcs::Thread::initial | ( | ) | [protected, virtual] |
void mcs::Thread::notify | ( | int | id, | |
Thread * | ref | |||
) | [protected, virtual] |
A method called from child threads to notify their termination.
If the address of a parent thread is provided in the constructor then the child can notify its parent of its termination through this method.
Note that this method will always be ran inside the child thread.
This method can be used to destroy Thread objects once their thread is terminated, for example:
void MyThread::notify(int id, Thread* ref) { cout << "Thread " << id << " is terminated." << endl; delete ref; }
id | Thread identifier of the terminating thread. | |
ref | Address of the Thread object whose thread is terminated. |
Reimplemented in mcs::Server.
Declared to avoid using of default assignment operator.
Thread * mcs::Thread::parent | ( | ) |
void mcs::Thread::run | ( | ) | [protected, virtual] |
Body of the thread execution.
When the new thread starts it will automatically execute the code in the run() method. This is the method you should overload in derived class to do the work you need.
Thread will be terminated once this method returns.
Reimplemented in mcs::ThreadFunc, mcs::UserThread, mcs::LocalThread, and mcs::Server.
void * mcs::Thread::RunThread | ( | void * | args | ) | [static, private] |
void mcs::Thread::set_cancel_state | ( | bool | cancel | ) | [protected] |
void mcs::Thread::start | ( | ) |
void mcs::Thread::startDetached | ( | bool | selfDelete = false |
) |
Start a new thread in the detached state.
If you start the thread using this method then you cannot stop the thread execution, it will terminate by itself.
This method will return only after the initial() method has been executed.
selfDelete | If set to true once the thread is terminated the object will be automatically destroyed. |
MCS_FATAL | MSG_CALLING_PTHREAD_CREATE; |
int mcs::Thread::state | ( | ) |
void mcs::Thread::stop | ( | ) |
void mcs::Thread::test_cancel | ( | ) | [protected] |
bool mcs::Thread::detached [private] |
Event* mcs::Thread::lerror [protected] |
int mcs::Thread::lid [private] |
Thread* mcs::Thread::lparent [private] |
int mcs::Thread::lstate [private] |
pthread_t mcs::Thread::lthrID [private] |
bool mcs::Thread::selfDelete [private] |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|