DetachedThread Class Reference

#include <Pt/System/Thread.h>

A detached thread. More...

Inherits Thread.

Public Types

enum  State {
  Ready = 0,
  Running = 1,
  Joined = 2,
  Detached = 3
 Status of a thread. More...

Public Member Functions

void detach ()
 Detaches the thread.
void init (const Callable< void > &cb)
 Initialize with a thread entry. More...
void join ()
 Wait for the thread to finish execution.
void start ()
 Starts the thread. More...
State state () const
 Returns the current state of the thread.

Static Public Member Functions

static void exit ()
 Exits athread. More...
static void sleep (unsigned int ms)
 Sleep for some time. More...
static void yield ()
 Yield CPU time. More...

Protected Member Functions

 DetachedThread ()
 Constructs a detached thread. More...
virtual void destroy ()
 Destroys a detached thread. More...
virtual void run ()
 Thread entry method. More...

Detailed Description

A detached thread runs just for its own. The user does not need (actually can not even) wait for the thread to stop. The object is normally created on the heap.


class MyThread : public Pt::System::::DetachedThread
void run();
void MyThread::run()
// implement, whatever needs to be done in parallel
void someFunc()
MyThread *thread = new MyThread();
// here the thread runs and the program can do something
// else in parallel. It continues to run even after this
// function returns. The object is automatically destroyed,
// when the thread has finished.

Member Enumeration Documentation

enum State

Not started yet.


Thread is running.


Joined with parent thread.


Detached from parent thread.

Constructor & Destructor Documentation

DetachedThread ( )

Constructs a thread object to execute the virtual method run() when start() is called. DetachedThreads are always destructed by the virtual method destroy(). If objects of this class are created by new, destroy() must be overloaded ti call delete.

Member Function Documentation

virtual void destroy ( )

This method is called after the thread has finished. The default implementation uses delete to destruct this object.

virtual void run ( )

This method is executed in a separate thread once start() is called. Override this method to implement a thread.

void init ( const Callable< void > &  cb)

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

void start ( )

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

static void exit ( )

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.

static void yield ( )

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

static void sleep ( unsigned int  ms)

The calling thread sleeps for ms milliseconds. Throws a SystemError on failure.