RobWorkProject  23.9.11-
Public Types | Public Member Functions | List of all members
ThreadPool Class Reference

A thread pool that can be assigned work. More...

#include <ThreadPool.hpp>

Public Types

typedef rw::core::Ptr< ThreadPoolPtr
 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.
 

Detailed Description

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.

Constructor & Destructor Documentation

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

Parameters
threadsnumber of threads to use - default is the number of hardware threads available on the system.

Member Function Documentation

◆ getQueueSize()

unsigned int getQueueSize ( )

Get the number of current tasks in the queue (tasks are removed from queue when done).

Returns
the number of current tasks.

◆ isStopping()

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.

Returns
true if thread should shut down.

◆ stop()

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.


The documentation for this class was generated from the following file: