hgl 0.5.25
A compiler/interpreter suite for developing images
Public Member Functions | List of all members
HGL::Common::ThreadPool< Worker, Task, NUM_THREADS > Class Template Reference

Template providing a thread pool for asynchronous tasks. More...

#include "thread_pool.h"

Inheritance diagram for HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >:
[legend]

Public Member Functions

void async (Task *const task)
 Run a task asynchrounously. More...
 
bool isRunning () const
 Checks if the thread pool is currently running. More...
 
void run (Task *const task)
 Queue and run a task. More...
 
size_t start (long num=2L)
 Starts the thread pool. More...
 
void stop ()
 Stops the thread pool. More...
 
void wait ()
 Waits for all tasks to get finished. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from HGL::Common::Singleton< T >
static T * getInstance ()
 Gets the single instance of T More...
 

Detailed Description

template<void(*)(void *) Worker, class Task, long NUM_THREADS = 64L>
class HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >

Template providing a thread pool for asynchronous tasks.

Template Parameters
Workerpointer to a function actually performing the task
Taska class containing the arguments for the Worker to do its job
NUM_THREADSmaximum number of threads to create
Author
Heiko Schäfer heiko.nosp@m.@hgl.nosp@m..rang.nosp@m.un.d.nosp@m.e

Member Function Documentation

template<void(*)(void *) Worker, class Task , long NUM_THREADS>
void HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >::async ( Task *const  task)

Run a task asynchrounously.

This method spawns only one thread and behaves basically like calling

* start();
* run(task);
* stop();
*

and stops the thread pool after the task is finished.

Parameters
taskthe task to run asynchrounously
Postcondition
it is safe to let the calling thread wait()
template<void(*)(void *) Worker, class Task , long NUM_THREADS>
bool HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >::isRunning ( ) const

Checks if the thread pool is currently running.

Returns
true if the thread pool is currently running, false otherwise
template<void(*)(void *) Worker, class Task , long NUM_THREADS>
void HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >::run ( Task *const  task)

Queue and run a task.

Parameters
taskthe task to queue and run
template<void(*)(void *) Worker, class Task , long NUM_THREADS>
size_t HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >::start ( long  num = 2L)

Starts the thread pool.

Parameters
numnumber of threads to start
Returns
the number of started threads
template<void(*)(void *) Worker, class Task , long NUM_THREADS>
void HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >::stop ( )

Stops the thread pool.

All queued tasks get finished and the thread pool stops

Postcondition
this call is neccessary to let the calling thread wait()
template<void(*)(void *) Worker, class Task , long NUM_THREADS>
void HGL::Common::ThreadPool< Worker, Task, NUM_THREADS >::wait ( )

Waits for all tasks to get finished.

Precondition
a call to stop() is neccessary if not waiting for an async job