A class to create separate threads.
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:
- in the joinable state (using the start() method): this way another thread (for example the parent) can stop the newly created thread while it is running.
- in the detached state (using the startDetached() method): this way the thread can only terminate by itself.
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 control reach the end of the overloaded run() method;
- an exception is thrown inside the overloaded run() method;
- the stop() method is called from another object/thread (applicable only if the thread has been started in the joinable state).
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:
- MCS_STATE_CREATED: the object has been created but the separate thread has not been started.
- MCS_STATE_RUNNING: the separate thread has executed initial() method and it is executing the run() method.
- MCS_STATE_TERMINATING: the run() method has returned or the stop() method has been called, and the thread is going to execute the final() method and eventually notify its parent of thread termination.
- MCS_STATE_END: the separate thread has executed the final() method, (eventually) notified its parent and terminated its execution.
Definition at line 2487 of file mcs.hh.
void mcs::Thread::notify |
( |
int |
id, |
|
|
Thread * |
ref |
|
) |
| |
|
protectedvirtual |
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;
}
- Parameters
-
id | Thread identifier of the terminating thread. |
ref | Address of the Thread object whose thread is terminated. |
Reimplemented in mcs::Server.
Definition at line 326 of file Thread.cc.