#include <cxxtools/thread.h>

Public Types | |
| enum | State { Ready = 0, Running = 1, Finished = 2 } |
| Status of a thread. More... | |
Public Member Functions | |
| virtual | ~Thread () |
| Destructor. | |
| State | state () const |
| Returns the current state of the thread. | |
| void | start () |
| Starts the thread. | |
| void | create () |
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. | |
Protected Member Functions | |
| Thread () | |
| Default Constructor. | |
| Thread (const Callable< void > &cb) | |
| Contructs a thread with a thread entry. | |
| void | init (const Callable< void > &cb) |
| Initialize with a thread entry. | |
| void | detach () |
| Detaches the thread. | |
| void | join () |
| Joins the thread. | |
| bool | joinNoThrow () |
| Joins the thread. | |
| void | terminate () |
| Terminates the thread. | |
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 the 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. A thread can be forced to terminate by calling terminate(), however, doing so is dangerous and discouraged.
Thread also provides a platform independent sleep function. A thread can give up CPU time either by calling Thread::yield() or sleep() to stop for a specified periode of time.
| enum State |
| Thread | ( | ) | [protected] |
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, protected] |
| virtual ~Thread | ( | ) | [virtual] |
The thread must either be joined or detached before the destructor is called.
| void init | ( | const Callable< void > & | cb | ) | [protected] |
The callable cb will be used as the thread entry. If another thread entry was set previously it will be replaced.
Referenced by DetachedThread::DetachedThread().
| 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.
Referenced by SpinMutex::lock().
| static void sleep | ( | unsigned int | ms | ) | [static] |
The calling thread sleeps for ms milliseconds. Throws a SystemError on failure.