Main Page   Class Hierarchy   Alphabetical List   Compound List   Compound Members  

util::Thread Class Reference

#include <Runnable.h>

Inheritance diagram for util::Thread:

util::Runnable util::Object List of all members.

Detailed Description

The Thread class is a convenient way of creating concurrently executing code that can use the same variables.

A thread works in the same environment as its parent. Mutexes must be used to protect access to shared variables.


Public Methods

 Thread (int detachState=PTHREAD_CREATE_JOINABLE)
 Create a new thread.

 ~Thread ()
void start (void) throw (RunException&)
 Start the thread.

void join (void) throw (RunException&)
 Wait this thread to die.

void detach (void) throw (RunException&)
 Detach this thread from its parent so that no one needs to "join" it to release the reserved resources.

void kill (int signal=SIGKILL) throw (RunException&)
 Send a signal to a thread.

void addThreadListener (ThreadListener *l)
 Add a listener that gets notified when the thread has finished its execution.

void removeThreadListener (ThreadListener *l)
 Remove a listener.

bool isRunning (void) const
 Check if the thread is currently running.


Protected Methods

void sleep (double secs) throw (RunException&)
 Suspend the execution of this thread for the specified number of seconds.

void sleep (unsigned int secs, unsigned int nanosecs) throw (RunException&)
 Suspend the execution of this thread for the specified number of seconds and nanoseconds.


Protected Attributes

bool _bRunning
 A flag that indicates whether the thread is currently executing or not.


Constructor & Destructor Documentation

util::Thread::Thread int    detachState = PTHREAD_CREATE_JOINABLE
 

Create a new thread.

Parameters:
detachState  can be either PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED. In the latter case, a started thread cannot be joined or detached.


Member Function Documentation

void util::Thread::addThreadListener ThreadListener   l [inline]
 

Add a listener that gets notified when the thread has finished its execution.

Parameters:
l  the listener to add to the list of interested listeners

bool util::Thread::isRunning void    const [inline]
 

Check if the thread is currently running.

Note that this method may return false when the thread is just being started. One should not rely on the return value exclusively when starting a thread, but catch the RunException& thrown by start() instead.

Returns:
true, if the thread is currently executing, false otherwise

void util::Thread::join void    throw (RunException&)
 

Wait this thread to die.

The calling thread will be blocked until this thread exists.

void util::Thread::kill int    signal = SIGKILL throw (RunException&)
 

Send a signal to a thread.

The default signal to be sent is SIGKILL which - by default - causes the thread to exit. One must ensure that the resources reserved by the killed thread are released. Especially mutexes must be handled with extreme care. Typically, however, it is not safe to kill any thread. Instead, one should consider setting an internal "cancellation" flag that is checked every now and then.

Parameters:
signal  the signal to send ('man signal' for more info)

void util::Thread::sleep unsigned int    secs,
unsigned int    nanosecs
throw (RunException&) [protected]
 

Suspend the execution of this thread for the specified number of seconds and nanoseconds.

Exceptions:
RunException  if the sleep is interrupted or nanosecs is greater than or equal to one billion.

void util::Thread::sleep double    secs throw (RunException&) [inline, protected]
 

Suspend the execution of this thread for the specified number of seconds.

Exceptions:
RunException  if the sleep is interrupted

void util::Thread::start void    throw (RunException&) [virtual]
 

Start the thread.

Exceptions:
RunException  & if the thread is already running or cannot be started for some other reason

Implements util::Runnable.


The documentation for this class was generated from the following files:
Documentation generated on 11.09.2003 with Doxygen.
The documentation is copyrighted material.
Copyright © Topi Mäenpää 2003. All rights reserved.