ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
AbstractThread Class Reference

Abstract for thread implementation. More...

#include <AbstractThread.hpp>

Inheritance diagram for AbstractThread:
Inheritance graph

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 &)
AbstractThreadoperator= (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.

Detailed Description

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:

class MyThread: public AbstractThread {
// Put thread arguments here
public:
MyThread() {
// Initialize arguments here
}
void run() {
// Put thread code here
// Access arguments as normal attributes
}
};
int main ()
{
MyThread t;
t.start();
t.waitForTermination();
}

In case a return value must be passed from the finished thread to the other thread, this can be achieved as follows:

class MyThread: public AbstractThread {
int returnValue_;
public:
MyThread(): returnValue_(0) {
// Initialize other arguments here
}
void run() {
// Put thread code here
returnValue_ = ...;
}
inline int getReturnValue() {
return returnValue_;
}
};
int main ()
{
MyThread t;
t.start();
t.waitForTermination();
// Attention: get return arguments only after the thread has
// terminated the execution, to avoid race conditions!
int ret = t.getReturnValue();
}

Definition at line 99 of file AbstractThread.hpp.

Constructor & Destructor Documentation

AbstractThread ( const AbstractThread )
private

Constructor. Initialize the class attributes.

Definition at line 51 of file AbstractThread.cpp.

:
isStarted_(false)
{
}
~AbstractThread ( )
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.

{
if (isStarted_){
WARNING("Killing a running thread!");
stop();
}
}

Here is the call graph for this function:

Member Function Documentation

bool blockSignal ( int  sig)
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

Parameters
sigthe signal to be blocked
Returns
true on success; false if some error occurred

Definition at line 137 of file AbstractThread.cpp.

{
sigset_t m;
sigemptyset(&m);
sigaddset(&m, sig);
if (pthread_sigmask(SIG_BLOCK, &m, NULL) != 0) {
ERROR("Can't mask signal " << sig);
return false;
}
return true;
}
static void checkTermination ( )
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.

{
pthread_testcancel();
}
void * Execute ( void *  param)
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.

Parameters
paramThe pointer to the concrete subclass.
Returns
Always zero.

Definition at line 40 of file AbstractThread.cpp.

{
AbstractThread* th = reinterpret_cast<AbstractThread*>(param);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
th->run();
return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool getSchedParam ( int *  policy,
int *  priority 
)

Get current scheduling policy and priority.

Parameters
policy,:policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER)
priority,:scheduling priority; it has a meaning only for SCHED_FIFO and SCHED_RR
Returns
true in case of success; false in case of error

Definition at line 251 of file AbstractThread.cpp.

{
struct sched_param p;
int ret = pthread_getschedparam(handle_, policy, &p);
*priority = p.sched_priority;
if (ret == 0)
return true;
else
return false;
}
AbstractThread& operator= ( const AbstractThread )
private
virtual void run ( )
protectedpure virtual

This function must be reimplemented in a derived class to define the specific function run by the thread.

Implemented in SimpleThread.

Here is the caller graph for this function:

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

Parameters
sigthe signal to be sent
Returns
true on success; false if some error occurred

Definition at line 178 of file AbstractThread.cpp.

{
if (pthread_kill(handle_, sig) != 0){
ERROR("Can't send signal " << sig);
return false;
}
return true;
}
bool setSchedParam ( int  policy,
int  priority 
)

Set scheduling policy and priority.

Parameters
policy,:policy (SCHED_FIFO, SCHED_RR or SCHED_OTHER)
priority,:scheduling priority
Returns
true in case of success; false in case of error

Definition at line 233 of file AbstractThread.cpp.

{
struct sched_param p;
p.sched_priority = priority;
if (pthread_setschedparam(handle_, policy, &p) == 0)
return true;
else
return false;
}
bool setSignalHandler ( int  sig,
void(*)(int)  handler 
)
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.

Parameters
sigthe signal to be sent
Returns
true on success; false if some error occurred

Definition at line 206 of file AbstractThread.cpp.

{
bool ret = true;
sigset_t oldset, set;
struct sigaction sa;
/* mask all signals until the handlers are installed */
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, &oldset);
bzero( &sa, sizeof(sa) );
sa.sa_handler = handler;
if (sigaction(sig, &sa, NULL) < 0) {
ERROR("Can't set signal " << sig);
ret = false;
}
/* remove the mask */
sigprocmask(SIG_SETMASK, &oldset,NULL);
return ret;
}
bool start ( )

Starts execution of the thread by calling run().

If the thread is already started this function does nothing.

Returns
true if the thread is started or it has been previously started; false if an error occurs when starting the thread.

Definition at line 78 of file AbstractThread.cpp.

{
return true;
if (pthread_create(&handle_, NULL, AbstractThread::Execute,
(void*)this) == 0)
isStarted_ = true;
return isStarted_;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool stop ( )

Stops the running thread.

Returns
true on success; false if an error occurs or the thread is not running (i.e., it has not been started or it has been already stopped)

Definition at line 96 of file AbstractThread.cpp.

{
if (!isStarted_){
DEBUG("Thread already stopped");
return false;
}
DEBUG("Cancelling thread...");
isStarted_ = false;
if (pthread_cancel(handle_) == 0){
DEBUG("Thread succesfully canceled.");
return true;
}
return false;
}

Here is the caller graph for this function:

bool unblockSignal ( int  sig)
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

Parameters
sigthe signal to be unblocked
Returns
true on success; false if some error occurred

Definition at line 158 of file AbstractThread.cpp.

{
sigset_t m;
sigemptyset(&m);
sigaddset(&m, sig);
if (pthread_sigmask(SIG_UNBLOCK, &m, NULL) != 0) {
ERROR("Can't unmask signal " << sig);
return false;
}
return true;
}
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

Returns
true on success, false if an error occurs.

Definition at line 119 of file AbstractThread.cpp.

{
if (pthread_join(handle_, NULL) == 0){
DEBUG("Thread succesfully joined.");
isStarted_ = false;
return true;
}
return false;
}

Here is the caller graph for this function:

Member Data Documentation

pthread_t handle_
protected

Definition at line 129 of file AbstractThread.hpp.

bool isStarted_
private

If the thread is running.

Definition at line 106 of file AbstractThread.hpp.


The documentation for this class was generated from the following files: