Home | C-Bits Package Documentation | Project Page |
#include <Thread.h>
Inheritance diagram for cbits::Thread:
Public Types | |
enum | State { READY = 1, ALIVE = 2, DEAD = 3 } |
Thread states. More... | |
enum | Priority { PRIORITY_1 = 1, PRIORITY_2 = 2, PRIORITY_3 = 3, PRIORITY_4 = 4, PRIORITY_5 = 5, PRIORITY_6 = 6, PRIORITY_7 = 7, PRIORITY_8 = 8, PRIORITY_9 = 9, MIN_PRIORITY = PRIORITY_1, DEFAULT_PRIORITY = PRIORITY_5, MAX_PRIORITY = PRIORITY_9 } |
Thread scheduling priorities. More... | |
Public Methods | |
void | start () |
Start the thread. More... | |
const long | getThreadID () const |
Get Thread's ID. More... | |
void | interrupt () |
Send a event to the thread. More... | |
const bool | join (const long msec=0) |
Wait for this thread to terminate. More... | |
const State | getState (const bool lockit=true) const |
Get this thread's run state. More... | |
void | setData (void *) const |
Associate user data with this thread. More... | |
void * | getData () const |
Retrieve user data that has been associated with this thread (if any). More... | |
virtual | ~Thread () |
Destructor. More... | |
Static Public Methods | |
Thread * | create (Runnable *r, const Priority p=DEFAULT_PRIORITY) |
Factory method for creating thread objects. More... | |
Thread * | getCurrent () |
Get reference to the current thread of execution. More... | |
void | yield () |
Give up the processor. More... | |
const bool | isInterrupted () |
Check the interrupt status of the current thread. More... | |
void | sleep (const long msec) |
Put current thread to sleep. More... | |
Protected Methods | |
Thread (Priority p=DEFAULT_PRIORITY) | |
Default constructor. More... | |
Thread (Runnable *r, Priority p=DEFAULT_PRIORITY) | |
Constructor. More... | |
Runnable * | getTarget () const |
Get a reference to the user-supplied execution object. More... | |
const State | setState (State, const bool lockit=true) |
Set the state of the thread. More... | |
void | setError (const char *e) const |
Set the error string that describes the most recent error. More... | |
const std::string & | getError () const |
Get the an explanation of the last error that occurred (if any). More... | |
const Priority | getPriority () const |
Get the thread's scheduling priority. More... | |
virtual const bool | impl_create_thread (const Priority)=0 |
Create a platform-specific thread. More... | |
virtual const bool | impl_start_thread ()=0 |
Start the platform-specific thread. More... | |
virtual const long | impl_get_threadid () const=0 |
Get thread's ID. More... | |
virtual void | impl_lock_obj () const=0 |
Lock this thread object. More... | |
virtual void | impl_unlock_obj () const=0 |
Unlock this thread object. More... | |
virtual void | impl_interrupt ()=0 |
Send an 'interrupt' event to the platform-specific thread implementation. More... | |
virtual const bool | impl_join (const long msec)=0 |
Wait for the platform thread to terminate. More... | |
virtual void | impl_sleep (const long msec)=0 |
Put the current thread to sleep. More... | |
Private Attributes | |
std::string | _error |
Description of most recent error. More... | |
Runnable * | _target |
User-supplied execution object. More... | |
Priority | _priority |
Thread's Scheduling priority. More... | |
State | _state |
Current state of the thread. More... | |
void * | _data |
Pointer to user-supplied opaque data object. More... | |
bool | _interrupted |
interrupt status of the thread. More... |
A cbits::Thread object represents an independent thread of execution through user supplied code.
To use cbits::Thread, do the following:
1. Subclass cbits::Runnable and implement cbits::Runnable::run() to carry out the thread's processing.
2. use cbits::Thread::create( Runnable* ) to create a thread instance, passing in the Runnable instance from step (1).
3. Invoke the 'start()' on the Thread instance created in step (2).
The thread should now be performing the actions implemented in the Runnable instance's run() method.
After the thread has started, the Thread::interrupt method should be used to tell the thread to stop. However, the logic encoded in the Runnable instance supplied to the Thread::create in step (2) above must be sensitive to the 'interrupt' status of the thread by periodically checking it using
Thread::getCurrent()->isInterrupted().
The Thread::join method can be used to 'wait' until the thread instance has exited.
For instance, the following code fragment creates a thread object, waits for a few seconds, interrupts the thread, and then waits for the thread to exit (The Runnable implementation is not shown):
Runnable* r = new ..; Thread* t = Thread::create( r ); t->start(); Thread::sleep( 5000 ); // milliseconds t->interrupt(); if( ! t->join( 60000 ); // wait up to 60 seconds { std::cerr << "Thread is unresponsive." << std::endl; } // ...
Implementation note:
A platform-specific derived class must be implemented for each platform that this class is ported to. The platform-specific derived class must inherit from this class and implement the protected and pure virtual impl_XXX methods (see below).
This class is influenced by the interface to Java's java.lang.Thread class.
|
Thread scheduling priorities.
|
|
Thread states.
|
|
Destructor.
|
|
Default constructor.
|
|
Constructor.
|
|
Factory method for creating thread objects.
|
|
Get reference to the current thread of execution.
|
|
Retrieve user data that has been associated with this thread (if any).
|
|
Get the an explanation of the last error that occurred (if any). This method is only available to the implementation-specific derived class.
|
|
Get the thread's scheduling priority. This method is only available to the implementation-specific derived class.
|
|
Get this thread's run state.
|
|
Get a reference to the user-supplied execution object.
|
|
Get Thread's ID.
|
|
Create a platform-specific thread. This method must be implemented by the platform-specific derived class.
Implemented in cbits::Thread_PTHREAD. |
|
Get thread's ID. This method is called internally to prevent access to the thread object while it is modifying its internal state.
Implemented in cbits::Thread_PTHREAD. |
|
Send an 'interrupt' event to the platform-specific thread implementation. This method must be implemented by the platform-specific derived class. Implemented in cbits::Thread_PTHREAD. |
|
Wait for the platform thread to terminate. This method must be implemented by the platform-specific derived class.
Implemented in cbits::Thread_PTHREAD. |
|
Lock this thread object. This method is called internally to prevent access to the thread object while it is modifying its internal state. This method must be implemented by the platform-specific derived class. Implemented in cbits::Thread_PTHREAD. |
|
Put the current thread to sleep. This method must be implemented by the platform-specific derived class. Implemented in cbits::Thread_PTHREAD. |
|
Start the platform-specific thread. This method must be implemented by the platform-specific derived class.
Implemented in cbits::Thread_PTHREAD. |
|
Unlock this thread object. This method is called internally to release the lock that may have been set to prevent access to the thread object while it is modifying its internal state. This method must be implemented by the platform-specific derived class. Implemented in cbits::Thread_PTHREAD. |
|
Send a event to the thread. It is intended that the Runnable code must be written in such a way that it is sensitive to the interrupt status of the thread. It may check the interrupt status by calling the 'isInterrupted' method. |
|
Check the interrupt status of the current thread. The interrupt state is reset after every call to this method.
|
|
Wait for this thread to terminate. FIXME Thread.h 'msec' argument is ignored by 'join' method.
|
|
Associate user data with this thread.
|
|
Set the error string that describes the most recent error. This method is only available to the implementation-specific derived class.
|
|
Set the state of the thread. This method is only available to the implementation specific derived class.
|
|
Put current thread to sleep.
|
|
Start the thread. The Thread object's state changes from READY to ALIVE. @raises ThreadException if :
|
|
Give up the processor. If no other threads are eligible to execute, the thread that yielded will be immediately rescheduled. Reimplemented in cbits::Thread_PTHREAD. |
|
Pointer to user-supplied opaque data object.
|
|
Description of most recent error.
|
|
interrupt status of the thread.
|
|
Thread's Scheduling priority.
|
|
Current state of the thread.
|
|
User-supplied execution object.
|
|
|