Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Static Public Member Functions | Protected Member Functions
icl::utils::Thread Class Reference

Simple object oriented thread class wrapping the pthread library. More...

#include <Thread.h>

Inheritance diagram for icl::utils::Thread:
icl::utils::ShallowCopyable< ThreadImpl, ThreadImplDelOp > icl::io::dc::DCGrabberThread icl::io::OpenNIGrabberThread icl::io::pylon::PylonGrabberThread icl::utils::ProcessMonitor

List of all members.

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
virtual 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
 returns the internal running state
bool runningNoLock () const
 returns the running state of the thread (without locking the mutex)

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

Detailed Description

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.

Performance

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


Constructor & Destructor Documentation

Create a new Thread.

virtual icl::utils::Thread::~Thread ( ) [virtual]

Destructor (if the thread is still running, it is ended)


Member Function Documentation

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

void icl::utils::Thread::lock ( ) [protected]

internal used lock function

This function (and unlock) can be used inside the reimplementation of the run function to enshure, that the code between lock() and unlock() is executed to the end befor the stop function is able to join the Thread

static void icl::utils::Thread::msleep ( unsigned int  msecs) [static]

sets the current thread to sleep for some milli-seconds

Parameters:
msecstime in msecs to sleep
virtual void icl::utils::Thread::run ( ) [pure virtual]

returns the internal running state

Internally, the implementation mutex will be locked here. This *might* cause issues

returns the running state of the thread (without locking the mutex)

this should usually be better (since less prone to producing deadlocks) then the running() method

static void icl::utils::Thread::sleep ( float  secs) [static]

sets the current thread to sleep for some seconds

Parameters:
secstime in secs to sleep ( float precision!)

starts the thread

if the thread is already running, an error message is shown

virtual void icl::utils::Thread::stop ( ) [virtual]

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

Returns:
zero if lock is acquired. otherwise an error-number
void icl::utils::Thread::unlock ( ) [protected]

internal used unlock function

static void icl::utils::Thread::usleep ( unsigned int  usec) [static]

just calling usleep

waits for this thread to be ended

The Thread ends automatically when its run function is over, or - if the run() function has not run to the end yet - it is ended when stop is called.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines