Image Component Library (ICL)
|
Simple object oriented thread class wrapping the pthread library. More...
#include <Thread.h>
Public Member Functions | |
Thread () | |
Create a new Thread. | |
virtual | ~Thread () |
Destructor (if the thread is still running, it is ended) | |
void | start () |
starts the thread | |
void | stop () |
stops the thread and waits till it is ended | |
void | wait () |
waits for this thread to be ended | |
virtual void | run ()=0 |
pure virtual run function doing all the work | |
virtual void | finalize () |
at the end of the stop function, this function is called | |
bool | running () const |
this function is not tested very well | |
Static Public Member Functions | |
static void | usleep (unsigned int usec) |
just calling usleep | |
static void | msleep (unsigned int msecs) |
sets the current thread to sleep for some milli-seconds | |
static void | sleep (float secs) |
sets the current thread to sleep for some seconds | |
Protected Member Functions | |
void | exit () |
exits the thread using pthread_exit (must be called from within the run function) | |
void | lock () |
internal used lock function | |
int | trylock () |
internal used trylock function | |
void | unlock () |
internal used unlock function |
Simple object oriented thread class wrapping the pthread library.
This Thread class is very simple to understand, and behaves essentially like Qts QThread. Create a custom Thread class derived from this class, reimplement the virtual run() function and use start and stop to contol the thread. a call to the threads start function will internally create a pthread which calls the Threads run() function a single time, so you have to add a while(1) statement to create a thread that is running until stop is called. Once a Thread run function returns, the corresponding pthread will call the Threads stop() function. Here is an example of a counter thread!
class CounterThread : public utils::Thread{ public: CounterThread(int n):n(n){} virtual void run(){ while(n>0){ lock(); n--; printf("%d cycles left\n",n); unlock(); sleep(1); } } private: int n; };
Additionally each Thread can be initialized with a given priority level. This feature is copied from Qt (version 4.2.x) and is not yet tested explicitly.
Although the Thread class implements the ShallowCopyable interface, it is desingned for optimal performance. On a 1.6GHz Pentium-M (linux), you can create, run, stop and release about 50.000 Threads per second. Thus it is possible to use this simple Thread implementation to create multi-threaded image processing modules as filters and so on.
TODO: Implement a dedicated class framework for this
Create a new Thread.
virtual icl::utils::Thread::~Thread | ( | ) | [virtual] |
Destructor (if the thread is still running, it is ended)
void icl::utils::Thread::exit | ( | ) | [protected] |
exits the thread using pthread_exit (must be called from within the run function)
virtual void icl::utils::Thread::finalize | ( | ) | [inline, virtual] |
at the end of the stop function, this function is called
Reimplemented in icl::io::dc::DCGrabberThread.
void icl::utils::Thread::lock | ( | ) | [protected] |
static void icl::utils::Thread::msleep | ( | unsigned int | msecs | ) | [static] |
sets the current thread to sleep for some milli-seconds
msecs | time in msecs to sleep |
virtual void icl::utils::Thread::run | ( | ) | [pure virtual] |
pure virtual run function doing all the work
Implemented in icl::io::dc::DCGrabberThread, icl::utils::ProcessMonitor, icl::io::pylon::PylonGrabberThread, and icl::io::OpenNIGrabberThread.
bool icl::utils::Thread::running | ( | ) | const |
this function is not tested very well
static void icl::utils::Thread::sleep | ( | float | secs | ) | [static] |
sets the current thread to sleep for some seconds
secs | time in secs to sleep ( float precision!) |
void icl::utils::Thread::start | ( | ) |
starts the thread
if the thread is already running, an error message is shown
void icl::utils::Thread::stop | ( | ) |
stops the thread and waits till it is ended
int icl::utils::Thread::trylock | ( | ) | [protected] |
internal used trylock function
works like lock but without blocking (it returns immediately).
void icl::utils::Thread::unlock | ( | ) | [protected] |
internal used unlock function
static void icl::utils::Thread::usleep | ( | unsigned int | usec | ) | [static] |
just calling usleep
void icl::utils::Thread::wait | ( | ) |