#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... | |
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.
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.
|
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.