libqi-api  2.1.4.13
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | List of all members
qi::ThreadPool Class Reference

Pool of workers. More...

#include <threadpool.hpp>

Public Member Functions

 ThreadPool (unsigned int minWorkers=2, unsigned int maxWorkers=100, unsigned int minIdleWorkers=1, unsigned int maxIdleWorkers=0)
 ThreadPool constructor. More...
 
 ~ThreadPool ()
 ThreadPool destructor. More...
 
unsigned int size () const
 Returns the number of workers in the pool. More...
 
unsigned int active () const
 Returns the number of active workers in the pool. More...
 
void setMaxWorkers (unsigned int n)
 Resizes the pool. More...
 
unsigned int getMaxWorkers () const
 Sets minimum number of workers in the pool. More...
 
void setMinWorkers (unsigned int n)
 Sets max idle workers. More...
 
unsigned int getMinWorkers () const
 Sets min idle workers. More...
 
void setMinIdleWorkers (unsigned int n)
 Returns maximum number of workers in the pool. More...
 
unsigned int getMinIdleWorkers () const
 Returns minimum number of workers in the pool. More...
 
void setMaxIdleWorkers (unsigned int n)
 Returns maximum number of inactive workers in the pool. More...
 
unsigned int getMaxIdleWorkers () const
 Returns minimum number of inactive workers in the pool. More...
 
void stop ()
 Stop the threadpool, no more will be accepted. More...
 
void reset ()
 Put the threadpool back in a state where it accept request. More...
 
void waitForAll ()
 Sleeps until all tasks are completed. More...
 
bool schedule (const boost::function< void(void)> &f)
 Adds a task to the pool. More...
 

Detailed Description

Pool of workers.

Definition at line 20 of file threadpool.hpp.

Constructor & Destructor Documentation

qi::ThreadPool::ThreadPool ( unsigned int  minWorkers = 2,
unsigned int  maxWorkers = 100,
unsigned int  minIdleWorkers = 1,
unsigned int  maxIdleWorkers = 0 
)

ThreadPool constructor.

Parameters
minWorkersMinimum number of workers in the pool at any time
maxWorkersMaximum number of workers allowed in the pool
minIdleWorkersMinimum number of inactive workers and ready to execute a task immediately
maxIdleWorkersMaximum number of workers inactive

If maxIdleWorkers is 0, then no thread reclamation is done.

qi::ThreadPool::~ThreadPool ( )

ThreadPool destructor.

* The destructor will quit all threads and return. All tasks left in the queue
* are dropped. To be sure that all tasks are completed, use
* :cpp:func:`qi::ThreadPool::waitForAll()`
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::size() const`, :cpp:func:`qi::ThreadPool::waitForAll()`
* 

Member Function Documentation

unsigned int qi::ThreadPool::active ( ) const

Returns the number of active workers in the pool.

Returns
Number of active workers.
qi::ThreadPool::getMaxIdleWorkers ( ) const

Returns minimum number of inactive workers in the pool.

Returns
Maximum mumber of inactive workers.
* Return the maximum number of inactive workers.
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMaxIdleWorkers(unsigned int)`
* 
qi::ThreadPool::getMaxWorkers ( ) const

Sets minimum number of workers in the pool.

Returns
Maximum number of workers.
* Return the maximum number of workers.
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMaxWorkers(unsigned int)`
* 
qi::ThreadPool::getMinIdleWorkers ( ) const

Returns minimum number of workers in the pool.

Returns
Minimum number of inactive workers.
* Return the minimum number of inactive workers.
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMinIdleWorkers(unsigned int)`
* 
qi::ThreadPool::getMinWorkers ( ) const

Sets min idle workers.

Returns
Minimum number of workers.
* Return the minimum number of workers.
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMinWorkers(unsigned int)`
* 
void qi::ThreadPool::reset ( )

Put the threadpool back in a state where it accept request.

qi::ThreadPool::schedule ( const boost::function< void(void)> &  f)

Adds a task to the pool.

Parameters
fBoost function with type void(void) that represents the task.
Returns
True if the task has been added to the tasks queue.

Adds a task to the pool. The task will begin immediately if there is at least one idling worker. Otherwise the tasks will be executed as soon as all tasks scheduled before have begun and a worker is idling.

qi::ThreadPool::setMaxIdleWorkers ( unsigned int  n)

Returns maximum number of inactive workers in the pool.

Parameters
nNew number of max idle workers in the pool.
* This function will change the maximum number of workers inactive in the pool.
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMinIdleWorkers(unsigned int)`, :cpp:func:`qi::ThreadPool::getMaxIdleWorkers() const`
* 
qi::ThreadPool::setMaxWorkers ( unsigned int  n)

Resizes the pool.

Parameters
nNew number of max workers in the pool
* This function will set the new maximum number of workers in the pool. It is
* impossible to set the maximum workers value to a lower value than the minimum
* one (value is discarded).
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMinWorkers(unsigned int)`, :cpp:func:`qi::ThreadPool::getMaxWorkers() const`
* 
qi::ThreadPool::setMinIdleWorkers ( unsigned int  n)

Returns maximum number of workers in the pool.

Parameters
nNew number of min idle workers in the pool.
* This function will change the minIdleWorkers number. It will spwan new
* workers if necessary and ensure that at any time, n workers are ready to
* handle new tasks.
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMaxIdleWorkers(unsigned int)`, :cpp:func:`qi::ThreadPool::getMinIdleWorkers() const`
* 
qi::ThreadPool::setMinWorkers ( unsigned int  n)

Sets max idle workers.

Parameters
nNew number of min workers in the pool
* This function will set the new minimum number of workers in the pool. It will
* spwan new workers if minWorkers is increased. It is impossible to set the
* minimum workers value to a greater value than maximum workers (value is
* discarded).
*
* .. seealso::
*     :cpp:func:`qi::ThreadPool::setMaxWorkers(unsigned int)`, :cpp:func:`qi::ThreadPool::getMinWorkers() const`
* 
qi::ThreadPool::size ( ) const

Returns the number of workers in the pool.

Returns
Number of workers.
void qi::ThreadPool::stop ( )

Stop the threadpool, no more will be accepted.

qi::ThreadPool::waitForAll ( )

Sleeps until all tasks are completed.

If all workers are inactive and there is no task left, returns immediately. Otherwise sleeps until all tasks in the pool are completed.


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