RobWorkProject
23.9.11-
|
A thread pool that can be assigned work. More...
#include <ThreadPool.hpp>
Public Types | |
typedef rw::core::Ptr< ThreadPool > | Ptr |
smart pointer type to this class | |
typedef boost::function< void(ThreadPool *)> | WorkFunction |
The type for a work function that can be assigned to the pool. | |
Public Member Functions | |
ThreadPool (int threads=-1) | |
Create new thread pool using the given number of threads. More... | |
virtual | ~ThreadPool () |
Destruct the pool and all threads (this calls the stop function first). | |
unsigned int | getNumberOfThreads () const |
Get number of threads in the pool. | |
void | stop () |
Stop processing more work in the queue, and try to stop running work if possible. More... | |
bool | isStopping () |
Check if work tasks are supposed to shut itself down. More... | |
void | addWork (WorkFunction work) |
Add work to the thread pool. | |
unsigned int | getQueueSize () |
Get the number of current tasks in the queue (tasks are removed from queue when done). More... | |
void | waitForEmptyQueue () |
Wait until the task queue becomes empty. | |
A thread pool that can be assigned work.
Work is handled in a FIFO manner, and the pool is intended to be very simple and basic.
For more complex behaviour please look at the ThreadTask type, which has a higher abstraction for adding tasks to a ThreadPool.
ThreadPool | ( | int | threads = -1 | ) |
Create new thread pool using the given number of threads.
If no argument is given, the maximum number of hardware threads on the system is used (number of CPUs, cores or hyperthreading units).
If number of threads is set to 0, the addWork function will be blocking (work is executed in thread that invokes addWork).
threads | number of threads to use - default is the number of hardware threads available on the system. |
unsigned int getQueueSize | ( | ) |
Get the number of current tasks in the queue (tasks are removed from queue when done).
bool isStopping | ( | ) |
Check if work tasks are supposed to shut itself down.
This function should be called from long-running worker functions to let them shut down gracefully.
void stop | ( | ) |
Stop processing more work in the queue, and try to stop running work if possible.
Long-running work should check if the isStopping function returns true and shut down gracefully.
Interrupts are issued, so if there is interruption points in the WorkFunction, the work should check for boost::thread_interrupted exceptions and shut down gracefully.