Thread Class Reference

A thread of execution. More...

#include <Thread.h++>

Inheritance diagram for Thread:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 Thread (bool detached=false, size_t stackSize=0)
 Construct a new Thread.
 Thread (Runnable *runnable, bool detached=false, size_t stackSize=0)
 Construct a new Thread with a Runnable object.
virtual ~Thread () throw ()
 Destructor.
virtual void start () throw ()
 Start executing the thread.
virtual void stop () throw ()
 Stop execution of the thread.
bool join () throw ()
 Wait for the thread to terminate.
bool isRunning () const throw ()
 Test if the thread is currently running.
bool isDetached () const throw ()
 Test if the thread is detached.
void setPriority (Priority priority) throw ()
 Set the thread priority.
Priority getPriority () const throw ()
 Get the thread priority.
void setName (const String &name) throw ()
 Set the name of this thread.
String getName () const throw ()
 Get the name of this thread.

Static Public Member Functions

static void sleep (timespan_ms_t msec) throw ()
 Suspend the calling thread for the given time interval.
static ThreadcurrentThread ()
 Obtain a pointer to the Thread object for the calling thread.
static ThreadID currentThreadID () throw ()
 Get the calling thread's thread ID.

Protected Member Functions

virtual void run ()
 Main function.
virtual void cleanup ()
 Cleanup function.
bool trySleep (timespan_ms_t msec) throw ()
 Suspend the thread for the given time interval, returning early if the thread was cancelled via a call to stop().
bool testCancel () throw ()
 Test for asynchronous cancellation requests.
void yield () throw ()
 Yield the CPU to (potentially) another thread.

Static Protected Member Functions

static void exit () throw ()
 Exit (terminate) the calling thread.

Detailed Description

A thread of execution.

Thread can be used in one of two ways:

A thread can be detached or joinable. A joinable thread continues to exist after it has finished executing; its resources must be reclaimed by calling the join() method. A joinable thread can be started and stopped multiple times.

Author:
Mark Lindner

Constructor & Destructor Documentation

Thread ( bool  detached = false,
size_t  stackSize = 0 
)

Construct a new Thread.

Parameters:
detached A flag indicating whether the thread will be created in a detached state.
stackSize The stack size for the thread, in bytes. A value of 0 indicates that the default stack size should be used.
Thread ( Runnable runnable,
bool  detached = false,
size_t  stackSize = 0 
)

Construct a new Thread with a Runnable object.

Parameters:
runnable The runnable object whose run() method will be executed in this thread.
detached A flag indicating whether the thread will be created in a detached state.
stackSize The stack size for the thread, in bytes. A value of 0 indicates that the default stack size should be used.
~Thread (  )  throw () [virtual]

Destructor.


Member Function Documentation

void cleanup (  )  [protected, virtual]

Cleanup function.

This function will be executed by the thread just prior to termination. The default implementation is a no-op.

Reimplemented in PulseTimer, and SocketMuxer.

Thread * currentThread (  )  [static]

Obtain a pointer to the Thread object for the calling thread.

If the calling thread is the main thread, or some other thread that was not created via commonc++, a NULL pointer is returned. Never delete the object returned by this method.

ccxx::ThreadID currentThreadID (  )  throw () [static]

Get the calling thread's thread ID.

void exit (  )  throw () [static, protected]

Exit (terminate) the calling thread.

String getName (  )  const throw () [inline]

Get the name of this thread.

Priority getPriority (  )  const throw ()

Get the thread priority.

bool isDetached (  )  const throw () [inline]

Test if the thread is detached.

bool isRunning (  )  const throw ()

Test if the thread is currently running.

bool join (  )  throw ()

Wait for the thread to terminate.

Returns:
true if the join was successful, false otherwise. Note that threads which were created in a detached state are not joinable.
void run (  )  [protected, virtual]

Main function.

Thread execution begins in this function. The default implementation does nothing.

Implements Runnable.

Reimplemented in DirectoryWatcher, PulseTimer, and SocketMuxer.

void setName ( const String name  )  throw () [inline]

Set the name of this thread.

void setPriority ( Priority  priority  )  throw ()

Set the thread priority.

void sleep ( timespan_ms_t  msec  )  throw () [static]

Suspend the calling thread for the given time interval.

Parameters:
msec The number of milliseconds to sleep.
void start (  )  throw () [virtual]

Start executing the thread.

If a Runnable object was supplied in the constructor, its run() method is invoked; otherwise the thread object's run() method is called. If the thread is already running, the call has no effect.

void stop (  )  throw () [virtual]

Stop execution of the thread.

The thread is not forcibly killed; instead, an asynchronous cancellation request is posted to the thread. If the thread is not running, the call has no effect.

bool testCancel (  )  throw () [protected]

Test for asynchronous cancellation requests.

Returns:
true If a cancellation request has been posted to the calling thread.
bool trySleep ( timespan_ms_t  msec  )  throw () [protected]

Suspend the thread for the given time interval, returning early if the thread was cancelled via a call to stop().

Parameters:
msec The number of milliseconds to sleep.
Returns:
true if the full interval elapsed, false if the thread was cancelled during the sleep.
void yield (  )  throw () [protected]

Yield the CPU to (potentially) another thread.


The documentation for this class was generated from the following files:
Generated on Sat Nov 26 16:49:09 2011 for libcommonc++ by  doxygen 1.6.3