Platform independent threads. More...

#include <cxxtools/thread.h>

Inheritance diagram for cxxtools::Thread:
cxxtools::NonCopyable cxxtools::AttachedThread cxxtools::DetachedThread

Public Types

enum  State { Ready = 0, Running = 1, Finished = 2 }
 Status of a thread. More...

Public Member Functions

virtual ~Thread ()
 Destructor.
State state () const
 Returns the current state of the thread.
void start ()
 Starts the thread.
void create ()

Static Public Member Functions

static void exit ()
 Exits athread.
static void yield ()
 Yield CPU time.
static void sleep (const Milliseconds &ts)
 Sleep for some time.

Protected Member Functions

 Thread ()
 Default Constructor.
 Thread (const Callable< void > &cb)
 Constructs a thread with a thread entry.
void init (const Callable< void > &cb)
 Initialize with a thread entry.
void detach ()
 Detaches the thread.
void join ()
 Joins the thread.
bool joinNoThrow ()
 Joins the thread.
void terminate ()
 Terminates the thread.
- Protected Member Functions inherited from cxxtools::NonCopyable
 NonCopyable ()

Detailed Description

Platform independent threads.

This is a thread base class, which is flexible, but hard to use. Try to use either an AttachedThread or a DetachedThread instead !!!

A Thread represents a separate thread of control within the program. It shares data with all the other threads within the process but executes independently in the way that a separate program does on a multitasking operating system. Each thread gets its own stack, which size is determinated by the operating system.

The execution of a thread starts either by calling the start() which calls the thread entry object passed to the constructor. Threads can either be joined, so you can wait for them, or be detached, so they run indepentently. A thread can be forced to terminate by calling terminate(), however, doing so is dangerous and discouraged.

Thread also provides a platform independent sleep function. A thread can give up CPU time either by calling Thread::yield() or sleep() to stop for a specified periode of time.

Member Enumeration Documentation

Status of a thread.

Enumerator:
Ready 

Thread was not started yet.

Running 

Thread was started.

Finished 

Thread has Finished.

Constructor & Destructor Documentation

cxxtools::Thread::Thread ( )
protected

Default Constructor.

Constructs a thread object without a thread entry. Use the init() method to set a callable. The thread will terminate immediately, if no thread entry is set.

cxxtools::Thread::Thread ( const Callable< void > &  cb)
explicitprotected

Constructs a thread with a thread entry.

Constructs a thread object to execute the Callable cb. The Thread is not started on construction, but when start() is called.

virtual cxxtools::Thread::~Thread ( )
virtual

Destructor.

The thread must either be joined or detached before the destructor is called.

Member Function Documentation

void cxxtools::Thread::create ( )
inline
void cxxtools::Thread::detach ( )
protected

Detaches the thread.

static void cxxtools::Thread::exit ( )
static

Exits athread.

This function is meant to be called from within a thread to leave the thread at once. Implicitly called when the thread entry is left. Throws a SystemError on failure.

void cxxtools::Thread::init ( const Callable< void > &  cb)
protected

Initialize with a thread entry.

The callable cb will be used as the thread entry. If another thread entry was set previously it will be replaced.

void cxxtools::Thread::join ( )
protected

Joins the thread.

Reimplemented in cxxtools::AttachedThread.

bool cxxtools::Thread::joinNoThrow ( )
protected

Joins the thread.

static void cxxtools::Thread::sleep ( const Milliseconds ts)
static

Sleep for some time.

The calling thread sleeps for a specified timespan. Since the Milliseconds class has conversion from double to Milliseconds it is possible to pass just a number. Another possibility is to pass another Timespan object. The cxxtools::Seconds is also derived from Timespan an hence perfectly ok.

Example:

cxxtools::Thread::sleep(500); // waits 500 milliseconds
cxxtools::Thread::sleep(cxxtools::Seconds(0.5)); // waits 500 milliseconds
void cxxtools::Thread::start ( )

Starts the thread.

This starts the execution of the thread by calling the thread entry. Throws a SystemError on failure.

State cxxtools::Thread::state ( ) const
inline

Returns the current state of the thread.

void cxxtools::Thread::terminate ( )
protected

Terminates the thread.

Reimplemented in cxxtools::AttachedThread.

static void cxxtools::Thread::yield ( )
static

Yield CPU time.

This function is meant to be called from within a thread to give up the CPU to other threads. Throws a SystemError on failure.


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