A thread pool that can execute arbitrary functions asynchronously. Functions with arguments to be executed are put in the queue through submit(). This returns a Pool::Job that can be used to retrieve the result or cancel the job if the result is no longer needed.
More...
#include <pool.h>
A thread pool that can execute arbitrary functions asynchronously. Functions with arguments to be executed are put in the queue through submit(). This returns a Pool::Job that can be used to retrieve the result or cancel the job if the result is no longer needed.
make_function implementation taken from: https://stackoverflow.com/questions/27825559/why-is-there-no-stdmake-function/27826081#27826081 TODO: Investigate other APIs for the submit() function in robowflex::Pool.
Definition at line 94 of file pool.h.
◆ Pool()
Constructor.
- Parameters
-
[in] | n | The number of threads to use. By default uses available hardware threads. |
Pool
Definition at line 25 of file pool.cpp.
27 for (
unsigned int i = 0; i < n; ++i)
bool active_
Is thread pool active?
std::vector< std::thread > threads_
Threads.
void run()
Background thread process. Executes jobs submitted from submit().
T emplace_back(T... args)
◆ ~Pool()
Destructor. Cancels all threads and joins them.
Definition at line 31 of file pool.cpp.
std::condition_variable cv_
Job queue condition variable.
◆ getThreadCount()
unsigned int Pool::getThreadCount |
( |
| ) |
const |
Get the number of threads.
- Returns
- The number of threads.
Definition at line 40 of file pool.cpp.
◆ run()
Background thread process. Executes jobs submitted from submit().
Definition at line 45 of file pool.cpp.
55 auto job =
jobs_.front();
61 if (!job->isCancled())
std::mutex mutex_
Job queue mutex.
std::queue< std::shared_ptr< Joblet > > jobs_
Jobs to execute.
◆ submit()
template<typename RT , typename... Args>
Submit a function with arguments to be processed by the thread pool. Submitted functions must be wrapped with robowflex::make_function() or be a std::function type so argument template deduction works.
- Parameters
-
[in] | function | Function to execute. |
[in] | args | Arguments to the function. |
- Template Parameters
-
RT | Return type of function. |
Args | Types of the arguments to the function. |
- Returns
- A job that contains information about the submitted function. This job can be canceled, which results in no execution of the function by the queue.
Definition at line 209 of file pool.h.
212 std::forward<Args>(args)...);
◆ active_
bool robowflex::Pool::active_ {false} |
|
private |
Is thread pool active?
Definition at line 230 of file pool.h.
◆ cv_
Job queue condition variable.
Definition at line 232 of file pool.h.
◆ jobs_
Jobs to execute.
Definition at line 235 of file pool.h.
◆ mutex_
◆ threads_
Threads.
Definition at line 234 of file pool.h.
The documentation for this class was generated from the following files: