Thread Class Reference

#include <Pt/System/Thread.h>

Portable thread of control. 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.
 Thread (const Callable< void > &cb)
 Constructs a thread with a thread entry.
 Thread (EventLoop &loop)
 Constructs a thread with an event loop.
void init (const Callable< void > &cb)
 Initialize with a thread entry.
virtual ~Thread ()
 Destructor.
State state () const
 Returns the current state of the thread.
void start ()
 Starts the thread.
void detach ()
 Detaches the thread.
void join ()
 Wait for the thread to finish execution.

Static Public Member Functions

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

Detailed Description

Thread is the portable thread. Construction does not start it. start() runs the Callable passed to the constructor or to init(), or it runs an EventLoop. The object must be joined or detached before it is destroyed. Destroying a running joinable thread is an error.

join() blocks until the entry returns. detach() lets the thread run without a waiter. After detach(), join() is not used. sleep() suspends the calling thread. yield() gives up the rest of the time slice. exit() leaves the current thread.

The callable must outlive a joinable thread. AttachedThread joins in its destructor. DetachedThread detaches in the constructor and destroys itself when the entry returns.

class Work
{
public:
void run()
{
}
};
int main()
{
Work work;
Pt::System::AttachedThread thread( Pt::callable(work, &Work::run) );
thread.start();
return 0;
}
ConstMethod< R, ClassT, As... > callable(ClassT &object, R(BaseT::*method)(As...) const)
Returns a ConstMethod object for the given object/method pair.
Definition ConstMethod.h:161
Thread that joins in its destructor.
Definition Thread.h:199

Member Enumeration Documentation

◆ State

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() [1/3]

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() [2/3]

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() [3/3]

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.

◆ ~Thread()

virtual ~Thread ( )
virtual

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

Member Function Documentation

◆ init()

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.

◆ start()

void start ( )

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

◆ exit()

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.

◆ yield()

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.

◆ sleep()

void sleep ( unsigned int ms)
static

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