Kea: isc::util::ThreadPool< WorkItem, Container > Struct Template Reference

| Kea 3.2.0-git

isc::util::ThreadPool< WorkItem, Container > Struct Template Reference

Defines a thread pool which uses a thread pool queue for managing work items. More...

`#include <thread_pool.h>`

## Public Types
typedef boost::shared_ptr< WorkItem > | WorkItemPtr
| Type of shared pointers to work items.

## Public Member Functions
| ThreadPool ()
| Constructor.
| ~ThreadPool ()
| Destructor.
bool | add (const WorkItemPtr &item)
| add a work item to the thread pool
bool | addFront (const WorkItemPtr &item)
| add a work item to the thread pool at front
size_t | count ()
| count number of work items in the queue
bool | enabled ()
| return the enable state of the queue
size_t | getMaxQueueSize ()
| get maximum number of work items in the queue
double | getQueueStat (size_t which)
| get queue length statistic
void | pause (bool wait=true)
| pause threads
bool | paused ()
| return the pause state of the queue
void | reset ()
| reset the thread pool stopping threads and clearing the internal queue
void | resume ()
| resume threads
void | setMaxQueueSize (size_t max_queue_size)
| set maximum number of work items in the queue
size_t | size ()
| size number of thread pool threads
void | start (uint32_t thread_count)
| start all the threads
void | stop ()
| stop all the threads
void | wait ()
| wait for current items to be processed
bool | wait (uint32_t seconds)
| wait for items to be processed or return after timeout

## Static Public Attributes
static const double | CEXP10
| Rounding value for 10 packet statistic.
static const double | CEXP100
| Rounding value for 100 packet statistic.
static const double | CEXP1000
| Rounding value for 1000 packet statistic.

## Detailed Description

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>
struct isc::util::ThreadPool< WorkItem, Container >

Defines a thread pool which uses a thread pool queue for managing work items.

Each work item is a 'functor' object.

Template Parameters

  WorkItem | a functor
  Container | a 'queue like' container

Definition at line 34 of file thread_pool.h.

## Member Typedef Documentation

## ◆ WorkItemPtr

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

typedef boost::shared_ptr<WorkItem> isc::util::ThreadPool< WorkItem, Container >::WorkItemPtr

Type of shared pointers to work items.

Definition at line 45 of file thread_pool.h.

## Constructor & Destructor Documentation

## ◆ ThreadPool()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

isc::util::ThreadPool< WorkItem, Container >::ThreadPool | ( |  | ) |
| inline

Constructor.

Definition at line 48 of file thread_pool.h.

## ◆ ~ThreadPool()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

isc::util::ThreadPool< WorkItem, Container >::~ThreadPool | ( |  | ) |
| inline

Destructor.

This destructor throws (when it must not) when called by a thread which is not the main one (e.g. a worker thread). This can't happen but static analyzers e.g. cppcheck do not know this so they complain.

Definition at line 57 of file thread_pool.h.

References reset().

Here is the call graph for this function:

## Member Function Documentation

## ◆ add()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

bool isc::util::ThreadPool< WorkItem, Container >::add | ( | const WorkItemPtr & | item | ) |
| inline

add a work item to the thread pool

Parameters

  item | the 'functor' object to be added to the queue

Returns
  false if the queue was full and oldest item(s) was dropped, true otherwise.

Definition at line 102 of file thread_pool.h.

Referenced by isc::dhcp::Dhcpv4Srv::processLocalizedQuery4(), isc::dhcp::Dhcpv6Srv::processLocalizedQuery6(), isc::dhcp::Dhcpv4Srv::selectSubnet(), isc::dhcp::Dhcpv6Srv::selectSubnet(), and isc::dhcp::Dhcpv4Srv::selectSubnet4o6().

## ◆ addFront()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

bool isc::util::ThreadPool< WorkItem, Container >::addFront | ( | const WorkItemPtr & | item | ) |
| inline

add a work item to the thread pool at front

Parameters

  item | the 'functor' object to be added to the queue

Returns
  false if the queue was full, true otherwise.

Definition at line 110 of file thread_pool.h.

Referenced by isc::dhcp::ClientHandler::~ClientHandler().

## ◆ count()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

size_t isc::util::ThreadPool< WorkItem, Container >::count | ( |  | ) |
| inline

count number of work items in the queue

Returns
  the number of work items in the queue

Definition at line 117 of file thread_pool.h.

## ◆ enabled()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

bool isc::util::ThreadPool< WorkItem, Container >::enabled | ( |  | ) |
| inline

return the enable state of the queue

The 'enabled' state corresponds to true value The 'disabled' state corresponds to false value

Returns
  the enable state of the queue

Definition at line 170 of file thread_pool.h.

## ◆ getMaxQueueSize()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

size_t isc::util::ThreadPool< WorkItem, Container >::getMaxQueueSize | ( |  | ) |
| inline

get maximum number of work items in the queue

Returns
  the maximum size (0 means unlimited)

Definition at line 194 of file thread_pool.h.

## ◆ getQueueStat()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

double isc::util::ThreadPool< WorkItem, Container >::getQueueStat | ( | size_t | which | ) |
| inline

get queue length statistic

Parameters

  which | select the statistic (10, 100 or 1000)

Returns
  the queue length statistic

Exceptions

  InvalidParameter | if which is not 10 and 100 and 1000.

Definition at line 210 of file thread_pool.h.

## ◆ pause()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

void isc::util::ThreadPool< WorkItem, Container >::pause | ( | bool | wait = true | ) |
| inline

pause threads

Used to pause threads so that they stop processing tasks

Parameters

  wait | the flag indicating if should wait for threads to pause.

Definition at line 153 of file thread_pool.h.

References wait().

Here is the call graph for this function:

## ◆ paused()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

bool isc::util::ThreadPool< WorkItem, Container >::paused | ( |  | ) |
| inline

return the pause state of the queue

The 'paused' state corresponds to true value The 'resumed' state corresponds to false value

Returns
  the pause state of the queue

Definition at line 180 of file thread_pool.h.

## ◆ reset()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

void isc::util::ThreadPool< WorkItem, Container >::reset | ( |  | ) |
| inline

reset the thread pool stopping threads and clearing the internal queue

It can be called several times even when the thread pool is stopped

Definition at line 65 of file thread_pool.h.

Referenced by ~ThreadPool().

## ◆ resume()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

void isc::util::ThreadPool< WorkItem, Container >::resume | ( |  | ) |
| inline

resume threads

Used to resume threads so that they start processing tasks

Definition at line 160 of file thread_pool.h.

## ◆ setMaxQueueSize()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

void isc::util::ThreadPool< WorkItem, Container >::setMaxQueueSize | ( | size_t | max_queue_size | ) |
| inline

set maximum number of work items in the queue

Parameters

  max_queue_size | the maximum size (0 means unlimited)

Definition at line 187 of file thread_pool.h.

Referenced by isc::util::MultiThreadingMgr::setPacketQueueSize().

## ◆ size()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

size_t isc::util::ThreadPool< WorkItem, Container >::size | ( |  | ) |
| inline

size number of thread pool threads

Returns
  the number of threads

Definition at line 201 of file thread_pool.h.

## ◆ start()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

void isc::util::ThreadPool< WorkItem, Container >::start | ( | uint32_t | thread_count | ) |
| inline

start all the threads

Parameters

  thread_count | specifies the number of threads to be created and started

Exceptions

  InvalidOperation | if thread pool already started
  InvalidParameter | if thread count is 0

Definition at line 77 of file thread_pool.h.

References isc_throw.

## ◆ stop()

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

void isc::util::ThreadPool< WorkItem, Container >::stop | ( |  | ) |
| inline

stop all the threads

Exceptions

  InvalidOperation | if thread pool already stopped

Definition at line 90 of file thread_pool.h.

References isc_throw.

## ◆ wait() [1/2]

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

void isc::util::ThreadPool< WorkItem, Container >::wait | ( |  | ) |
| inline

wait for current items to be processed

Used to block the calling thread until all items in the queue have been processed

Definition at line 125 of file thread_pool.h.

References isc_throw.

Referenced by pause().

## ◆ wait() [2/2]

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

bool isc::util::ThreadPool< WorkItem, Container >::wait | ( | uint32_t | seconds | ) |
| inline

wait for items to be processed or return after timeout

Used to block the calling thread until all items in the queue have been processed or return after timeout

Parameters

  seconds | the time in seconds to wait for tasks to finish

Returns
  true if all tasks finished, false on timeout

Definition at line 140 of file thread_pool.h.

References isc_throw.

## Member Data Documentation

## ◆ CEXP10

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

const double isc::util::ThreadPool< WorkItem, Container >::CEXP10
| static

Rounding value for 10 packet statistic.

Definition at line 36 of file thread_pool.h.

## ◆ CEXP100

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

const double isc::util::ThreadPool< WorkItem, Container >::CEXP100
| static

Rounding value for 100 packet statistic.

Definition at line 39 of file thread_pool.h.

## ◆ CEXP1000

template<typename WorkItem, typename Container = std::deque<boost::shared_ptr<WorkItem>>>

const double isc::util::ThreadPool< WorkItem, Container >::CEXP1000
| static

Rounding value for 1000 packet statistic.

Definition at line 42 of file thread_pool.h.

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

- thread_pool.h