MRPT  2.0.4
List of all members | Public Types | Public Member Functions | Private Attributes
mrpt::system::WorkerThreadsPool Class Reference

Detailed Description

A simple thread pool.

Note
Partly based on: https://github.com/progschj/ThreadPool (ZLib license)

Definition at line 29 of file WorkerThreadsPool.h.

#include <mrpt/system/WorkerThreadsPool.h>

Public Types

enum  queue_policy_t : uint8_t { POLICY_FIFO, POLICY_DROP_OLD }
 

Public Member Functions

 WorkerThreadsPool ()=default
 
 WorkerThreadsPool (std::size_t num_threads, queue_policy_t p=POLICY_FIFO)
 
 ~WorkerThreadsPool ()
 
void resize (std::size_t num_threads)
 
void clear ()
 Stops all working jobs. More...
 
template<class F , class... Args>
auto enqueue (F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
 Enqueue one new working item, to be executed by threads when any is available. More...
 
std::size_t pendingTasks () const noexcept
 Returns the number of enqueued tasks, currently waiting for a free working thread to process them. More...
 

Private Attributes

std::vector< std::thread > threads_
 
std::atomic_bool do_stop_ {false}
 
std::mutex queue_mutex_
 
std::condition_variable condition_
 
std::queue< std::function< void()> > tasks_
 
queue_policy_t policy_ {POLICY_FIFO}
 

Member Enumeration Documentation

◆ queue_policy_t

Enumerator
POLICY_FIFO 

Default policy: all tasks are executed in FIFO order.

POLICY_DROP_OLD 

If a task arrives and there are more pending tasks than worker threads, drop previous tasks.

Definition at line 32 of file WorkerThreadsPool.h.

Constructor & Destructor Documentation

◆ WorkerThreadsPool() [1/2]

mrpt::system::WorkerThreadsPool::WorkerThreadsPool ( )
default

◆ WorkerThreadsPool() [2/2]

mrpt::system::WorkerThreadsPool::WorkerThreadsPool ( std::size_t  num_threads,
queue_policy_t  p = POLICY_FIFO 
)
inline

Definition at line 42 of file WorkerThreadsPool.h.

References resize().

Here is the call graph for this function:

◆ ~WorkerThreadsPool()

mrpt::system::WorkerThreadsPool::~WorkerThreadsPool ( )
inline

Definition at line 47 of file WorkerThreadsPool.h.

References clear().

Here is the call graph for this function:

Member Function Documentation

◆ clear()

void WorkerThreadsPool::clear ( )

Stops all working jobs.

Definition at line 18 of file WorkerThreadsPool.cpp.

References condition_, do_stop_, queue_mutex_, tasks_, and threads_.

Referenced by ~WorkerThreadsPool().

Here is the caller graph for this function:

◆ enqueue()

template<class F , class... Args>
auto mrpt::system::WorkerThreadsPool::enqueue ( F &&  f,
Args &&...  args 
) -> std::future<typename std::result_of<F(Args...)>::type>

Enqueue one new working item, to be executed by threads when any is available.

Definition at line 71 of file WorkerThreadsPool.h.

◆ pendingTasks()

std::size_t WorkerThreadsPool::pendingTasks ( ) const
noexcept

Returns the number of enqueued tasks, currently waiting for a free working thread to process them.

Definition at line 36 of file WorkerThreadsPool.cpp.

References tasks_.

◆ resize()

void WorkerThreadsPool::resize ( std::size_t  num_threads)

Definition at line 41 of file WorkerThreadsPool.cpp.

References threads_.

Referenced by WorkerThreadsPool().

Here is the caller graph for this function:

Member Data Documentation

◆ condition_

std::condition_variable mrpt::system::WorkerThreadsPool::condition_
private

Definition at line 65 of file WorkerThreadsPool.h.

Referenced by clear().

◆ do_stop_

std::atomic_bool mrpt::system::WorkerThreadsPool::do_stop_ {false}
private

Definition at line 63 of file WorkerThreadsPool.h.

Referenced by clear().

◆ policy_

queue_policy_t mrpt::system::WorkerThreadsPool::policy_ {POLICY_FIFO}
private

Definition at line 67 of file WorkerThreadsPool.h.

◆ queue_mutex_

std::mutex mrpt::system::WorkerThreadsPool::queue_mutex_
private

Definition at line 64 of file WorkerThreadsPool.h.

Referenced by clear().

◆ tasks_

std::queue<std::function<void()> > mrpt::system::WorkerThreadsPool::tasks_
private

Definition at line 66 of file WorkerThreadsPool.h.

Referenced by clear(), and pendingTasks().

◆ threads_

std::vector<std::thread> mrpt::system::WorkerThreadsPool::threads_
private

Definition at line 62 of file WorkerThreadsPool.h.

Referenced by clear(), and resize().




Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020