Image Component Library (ICL)
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
icl::utils::Thread Class Referenceabstract

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

Public Member Functions

 Thread ()
 Create a new Thread. More...
 
virtual ~Thread ()
 Destructor (if the thread is still running, it is ended) More...
 
void start ()
 starts the thread More...
 
virtual void stop ()
 stops the thread and waits till it is ended More...
 
void wait ()
 waits for this thread to be ended More...
 
virtual void run ()=0
 pure virtual run function doing all the work More...
 
virtual void finalize ()
 at the end of the stop function, this function is called More...
 
bool running () const
 returns the internal running state More...
 
bool runningNoLock () const
 returns the running state of the thread (without locking the mutex) More...
 
- Public Member Functions inherited from icl::utils::ShallowCopyable< ThreadImpl, ThreadImplDelOp >
bool isNull () const
 returns wheter the objects implementation holds a null pointer More...
 

Static Public Member Functions

static void usleep (unsigned int usec)
 just calling usleep More...
 
static void msleep (unsigned int msecs)
 sets the current thread to sleep for some milli-seconds More...
 
static void sleep (float secs)
 sets the current thread to sleep for some seconds More...
 

Protected Member Functions

void exit ()
 exits the thread using pthread_exit (must be called from within the run function) More...
 
void lock ()
 internal used lock function More...
 
int trylock ()
 internal used trylock function More...
 
void unlock ()
 internal used unlock function More...
 
void join ()
 internal used join function More...
 
- Protected Member Functions inherited from icl::utils::ShallowCopyable< ThreadImpl, ThreadImplDelOp >
 ShallowCopyable (ThreadImpl *t=0)
 create a the implementation with a given T* value More...
 

Additional Inherited Members

- Public Types inherited from icl::utils::ShallowCopyable< ThreadImpl, ThreadImplDelOp >
typedef ShallowCopyable< ThreadImpl, ThreadImplDelOp > ParentSC
 
- Protected Attributes inherited from icl::utils::ShallowCopyable< ThreadImpl, ThreadImplDelOp >
SmartPtrBase< ThreadImpl, ThreadImplDelOp > impl
 shared pointer for the classes implementation More...
 

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

◆ Thread()

icl::utils::Thread::Thread ( )

Create a new Thread.

◆ ~Thread()

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

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

Member Function Documentation

◆ exit()

void icl::utils::Thread::exit ( )
protected

exits the thread using pthread_exit (must be called from within the run function)

◆ finalize()

virtual void icl::utils::Thread::finalize ( )
inlinevirtual

at the end of the stop function, this function is called

◆ join()

void icl::utils::Thread::join ( )
protected

internal used join function

◆ lock()

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

◆ msleep()

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

◆ run()

virtual void icl::utils::Thread::run ( )
pure virtual

◆ running()

bool icl::utils::Thread::running ( ) const

returns the internal running state

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

◆ runningNoLock()

bool icl::utils::Thread::runningNoLock ( ) const

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

◆ sleep()

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

◆ start()

void icl::utils::Thread::start ( )

starts the thread

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

◆ stop()

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

stops the thread and waits till it is ended

◆ trylock()

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

◆ unlock()

void icl::utils::Thread::unlock ( )
protected

internal used unlock function

◆ usleep()

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

just calling usleep

◆ wait()

void icl::utils::Thread::wait ( )

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: