ONPOSIX
2.0
|
Abstract for thread implementation. More...
#include <AbstractThread.hpp>
Public Member Functions | |
AbstractThread () | |
Constructor. Initialize the class attributes. | |
virtual | ~AbstractThread () |
Destructor. | |
bool | start () |
Starts execution of the thread by calling run(). | |
bool | stop () |
Stops the running thread. | |
bool | waitForTermination () |
Blocks the calling thread until the thread is finished. | |
bool | sendSignal (int sig) |
Sends a signal to the thread. | |
bool | setSchedParam (int policy, int priority) |
Set scheduling policy and priority. | |
bool | getSchedParam (int *policy, int *priority) |
Get current scheduling policy and priority. |
Static Public Member Functions | |
static bool | blockSignal (int sig) |
Masks a specific signal on this thread. | |
static bool | unblockSignal (int sig) |
Unmasks a signal previously masked. | |
static bool | setSignalHandler (int sig, void(*handler)(int)) |
Set a handler for a specific signal. |
Protected Member Functions | |
virtual void | run ()=0 |
This function must be reimplemented in a derived class to define the specific function run by the thread. |
Static Protected Member Functions | |
static void | checkTermination () |
Function to know if the thread has finished computation. |
Protected Attributes | |
pthread_t | handle_ |
Private Member Functions | |
AbstractThread (const AbstractThread &) | |
AbstractThread & | operator= (const AbstractThread &) |
Static Private Member Functions | |
static void * | Execute (void *param) |
The static function representing the code executed in the thread context. |
Private Attributes | |
bool | isStarted_ |
If the thread is running. |
Abstract for thread implementation.
Base abstract non copyable class for thread implementation. Derived classes must implement the pure virtual function run() to define their specific behavior. This class makes use of POSIX threads (pthreads). The thread starts stopped. To start it, the start() method must be explicitly called.
Example of usage:
In case a return value must be passed from the finished thread to the other thread, this can be achieved as follows:
Definition at line 99 of file AbstractThread.hpp.
|
private |
AbstractThread | ( | ) |
Constructor. Initialize the class attributes.
Definition at line 51 of file AbstractThread.cpp.
|
virtual |
Destructor.
In case the thread is running, it stops the thread and prints an error message.
Definition at line 63 of file AbstractThread.cpp.
|
static |
Masks a specific signal on this thread.
This method allows to block a specific signal The list of signals is available on /usr/include/bits/signum.h
sig | the signal to be blocked |
Definition at line 137 of file AbstractThread.cpp.
|
inlinestaticprotected |
Function to know if the thread has finished computation.
This function can be used in the subclasses to check if a request for termination has been made; if so, the thread is terminated.
Definition at line 119 of file AbstractThread.hpp.
|
staticprivate |
The static function representing the code executed in the thread context.
This function just calls the run() function of the subclass. We need this level of indirection because pthread_create() cannot accept a method which is non static or virtual.
param | The pointer to the concrete subclass. |
Definition at line 40 of file AbstractThread.cpp.
bool getSchedParam | ( | int * | policy, |
int * | priority | ||
) |
Get current scheduling policy and priority.
policy,: | policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER) |
priority,: | scheduling priority; it has a meaning only for SCHED_FIFO and SCHED_RR |
Definition at line 251 of file AbstractThread.cpp.
|
private |
|
protectedpure virtual |
This function must be reimplemented in a derived class to define the specific function run by the thread.
Implemented in SimpleThread.
bool sendSignal | ( | int | sig | ) |
Sends a signal to the thread.
This method allows to send a signal from one thread to another thread The list of signals is available on /usr/include/bits/signum.h
sig | the signal to be sent |
Definition at line 178 of file AbstractThread.cpp.
bool setSchedParam | ( | int | policy, |
int | priority | ||
) |
Set scheduling policy and priority.
policy,: | policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER) |
priority,: | scheduling priority |
Definition at line 233 of file AbstractThread.cpp.
|
static |
Set a handler for a specific signal.
This method allows to manually set a handler for handling a specific signal. The list of signals is available on /usr/include/bits/signum.h Use signals less as possible, mainly for standard situations. During the execution of the handler other signals may arrive. This can lead to inconsistent states. The handler must be short. It must just update the internal state and/or kill the application. Not all library functions can be called inside the handler without having strange behaviors (see man signal). In particular, it's not safe calling functions of the standard library, like printf() or exit(), or other functions defined inside the application itself. The access to global variables is not safe either, unless they have been defined as volatile.
sig | the signal to be sent |
Definition at line 206 of file AbstractThread.cpp.
bool start | ( | ) |
Starts execution of the thread by calling run().
If the thread is already started this function does nothing.
Definition at line 78 of file AbstractThread.cpp.
bool stop | ( | ) |
Stops the running thread.
Definition at line 96 of file AbstractThread.cpp.
|
static |
Unmasks a signal previously masked.
This method allows to unblock a specific signal previously blocked through blockSignal(). The list of signals is available on /usr/include/bits/signum.h
sig | the signal to be unblocked |
Definition at line 158 of file AbstractThread.cpp.
bool waitForTermination | ( | ) |
Blocks the calling thread until the thread is finished.
This method blocks the calling thread until the thread associated with the AbstractThread object has finished execution
Definition at line 119 of file AbstractThread.cpp.
|
protected |
Definition at line 129 of file AbstractThread.hpp.
|
private |
If the thread is running.
Definition at line 106 of file AbstractThread.hpp.