#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. | |
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.
| enum State |
| Enumerator | |
|---|---|
| Ready | Not started yet. |
| Running | Thread is running. |
| Joined | Joined with parent thread. |
| Detached | Detached from parent thread. |
| 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.
|
explicit |
|
explicit |
|
virtual |
The thread must either be joined or detached before the destructor is called.
| 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 |
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 |
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 |
The calling thread sleeps for ms milliseconds. Throws a SystemError on failure.