UtiLite
0.3.1
A lite utilities library
|
#include <UThread.h>
Inherited by UAbstractImageCapture, UAudioCapture, UAudioCaptureFFT, UAudioPlayerTone, UEventDispatcher, UEventsManager, and UObjDeletionThread< T >.
Public Types | |
enum | Priority |
Public Member Functions | |
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 |
Private Member Functions | |
virtual void | mainLoopBegin () |
virtual void | mainLoop ()=0 |
virtual void | mainLoopKill () |
virtual void | mainLoopEnd () |
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() { // The calling thread will wait until this thread has finished. this->join(true); } protected: virtual void mainLoop() { // Do some works... // This will stop the thread, otherwise the mainLoop() is recalled. this->kill(); } }; int main(int argc, char * argv[]) { SimpleThread t; t.start(); t.join(); // Wait until the thread has finished. return 0; }
enum UThread::Priority |
Enum of priorities : kPLow, kPBelowNormal, kPNormal, kPAboveNormal, kPRealTime.
UThread::UThread | ( | Priority | priority = kPNormal | ) |
UThread::~UThread | ( | ) | [virtual] |
The destructor. Inherited classes must call join(true) inside their destructor to avoid memory leaks where the underlying c-thread is still running.
Note: not safe to delete a thread while other threads are joining it.
bool UThread::isCreating | ( | ) | const |
bool UThread::isIdle | ( | ) | const |
bool UThread::isKilled | ( | ) | const |
bool UThread::isRunning | ( | ) | const |
void UThread::join | ( | bool | killFirst = false | ) |
The caller thread will wait until the thread has finished.
Note : blocking call
killFirst | if you want kill() to be called before joining (default false), otherwise not. |
void UThread::kill | ( | ) |
Kill the thread. This functions does nothing if the thread is not started or is killed.
Note : not a blocking call
virtual void UThread::mainLoop | ( | ) | [private, pure virtual] |
Pure virtual method mainLoop(). The inner loop of the thread. This method is called repetitively until the thread is killed. Note that if kill() is called in mainLoopBegin(), mainLoop() is not called, terminating immediately the thread.
Implemented in UEventsManager, UObjDeletionThread< T >, UAudioCaptureMic, UAbstractImageCapture, UAudioCaptureFFT, UAudioCaptureFile, UAudioPlayerTone, and UEventDispatcher.
virtual void UThread::mainLoopBegin | ( | ) | [inline, private, virtual] |
Virtual method mainLoopBegin(). User can implement this function to add a behavior before the main loop is started. It is called once (before entering mainLoop()).
Reimplemented in UAudioCaptureMic, UAbstractImageCapture, UAudioCaptureFFT, UAudioCaptureFile, and UAudioPlayerTone.
virtual void UThread::mainLoopEnd | ( | ) | [inline, private, virtual] |
Virtual method mainLoopEnd(). User can implement this function to add a behavior after the thread is killed (after exiting the mainLoop(), work is still done in the thread before exiting).
Reimplemented in UAudioCapture, UAudioCaptureMic, UAudioCaptureFile, and UAudioPlayerTone.
virtual void UThread::mainLoopKill | ( | ) | [inline, private, virtual] |
Virtual method mainLoopKill(). User can implement this function to add a behavior before the thread is killed. When this function is called, the state of the thread is set to kSKilled. It is useful to wake up a sleeping thread to finish his loop and to avoid a deadlock.
Reimplemented in UEventsManager, and UAudioCaptureFFT.
void UThread::setAffinity | ( | int | cpu = 0 | ) |
Set the thread affinity. This is applied during start of the thread.
MAC OS X : http://developer.apple.com/library/mac/#releasenotes/Performance/RN-AffinityAPI/_index.html.
cpu | the cpu id (start at 1), 0 means no affinity (default). |
void UThread::setPriority | ( | Priority | priority | ) |
Set the thread priority.
priority | the priority |
void UThread::start | ( | ) |