|
| | UThread (Priority priority=kPNormal) |
| |
| virtual | ~UThread () |
| |
| void | start () |
| |
| void | kill () |
| |
| void | join (bool killFirst=false) |
| |
| void | setPriority (Priority priority) |
| |
| void | setAffinity (int cpu=0) |
| |
| bool | isCreating () const |
| |
| bool | isRunning () const |
| |
| bool | isIdle () const |
| |
| bool | isKilled () const |
| |
| Handle | getThreadHandle () const |
| |
| unsigned long | getThreadId () const |
| |
| int | Create (Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const |
| |
| int | Create (unsigned long &ThreadId, Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const |
| |
| int | Create (Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const |
| |
| int | Create (unsigned long &ThreadId, Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const |
| |
The class UThread is an abstract class for creating thread objects. A UThread provides methods to create threads as an object-style fashion.
For most of inherited classes, only mainLoop() needs to be implemented, then only start() needs to be called from the outside. The main loop is called until the thread itself calls kill() or another thread calls kill() or join() (with parameter to true) on this thread. Unlike kill(), join() is a blocking call: the calling thread will wait until this thread has finished, thus join() must not be called inside the mainLoop().
If inside the mainLoop(), at some time, the thread needs to wait on a mutex/semaphore (like for the acquisition of a resource), the function mainLoopKill() should be implemented to release the mutex/semaphore when the thread is killed, to avoid a deadlock. The function killCleanup() is called after the thread's state is set to kSKilled. After the mutex/semaphore is released in killCleanup(), on wake up, the thread can know if it needs to stop by calling isKilled().
To do an initialization process (executed by the worker thread) just one time before entering the mainLoop(), mainLoopBegin() can be implemented.
Example:
#include "utilite/UThread.h"
class SimpleThread :
public UThread
{
public:
SimpleThread() {}
virtual ~SimpleThread() {
this->join(true);
}
protected:
virtual void mainLoop() {
this->kill();
}
};
int main(int argc, char * argv[])
{
SimpleThread t;
t.start();
t.join();
return 0;
}
- See also
- start()
-
kill()
-
join()
-
mainLoopBegin()
-
mainLoopKill()
-
mainLoop()
Definition at line 86 of file UThread.h.