AttachedThread Class Reference

#include <Pt/System/Thread.h>

A Joinable thread. More...

Inherits Thread.

Public Types

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

Public Member Functions

 AttachedThread ()
 Default Constructor. More...
 
 AttachedThread (const Callable< void > &cb)
 Constructs a thread with a thread entry. More...
 
 AttachedThread (EventLoop &loop)
 Constructs a thread with an event loop. More...
 
 ~AttachedThread ()
 Joins the thread, if not already joined.
 
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...
 

Detailed Description

AttachedThreads are threads, which are managed by the creator, and are normally created on the stack. The creator must wait, until the thread terminates either explicitly by calling join() or implicitly by the destructor. The life-time of the callable object must exceed the life-time of the thread. Mind the order of destruction if the AttachedThread is a member variable of a class.

Example:

struct Operation
{
void run()
{
// implement, whatever needs to be done in parallel
}
};
int main()
{
Operation op;
AttachedThread thread( Pt::callable(op, &Operation::run) );
thread.start();
// the thread runs and we can do something else in parallel
doMoreWork();
// AttachedThread's destructor joins the thread
return 0;
}

Member Enumeration Documentation

enum State
inherited
Enumerator
Ready 

Not started yet.

Running 

Thread is running.

Joined 

Joined with parent thread.

Detached 

Detached from parent thread.

Constructor & Destructor Documentation

Constructs a thread object without a thread entry. Use the init() method to set a callable.

AttachedThread ( const Callable< void > &  cb)
explicit

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

AttachedThread ( EventLoop loop)
explicit

Constructs a thread object to run the event loop loop in a separate thread. The thread is not started on construction, but when start() is called.

Member Function Documentation

void init ( const Callable< void > &  cb)
inherited

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

void start ( )
inherited

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

static void exit ( )
staticinherited

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 ( )
staticinherited

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)
staticinherited

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