Thread Class Reference

#include <Pt/System/Thread.h>

Platform independent threads. More...

Inherits NonCopyable.

Inherited by AttachedThread, and DetachedThread.

Public Types

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

Public Member Functions

 Thread ()
 Default Constructor. More...
 
 Thread (const Callable< void > &cb)
 Constructs a thread with a thread entry. More...
 
 Thread (EventLoop &loop)
 Constructs a thread with an event loop. More...
 
virtual ~Thread ()
 Destructor. More...
 
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

This is a thread base class, which is flexible, but harder 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 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.

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

Member Enumeration Documentation

enum State
Enumerator
Ready 

Not started yet.

Running 

Thread is running.

Joined 

Joined with parent thread.

Detached 

Detached from parent thread.

Constructor & Destructor Documentation

Thread ( )

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.

Thread ( 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.

Thread ( 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.

virtual ~Thread ( )
virtual

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

Member Function Documentation

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

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

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

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