#include <threads.hpp>
|
| ThreadPool () |
| Default constructor that creates a ThreadPool with the number of threads. More...
|
|
| ThreadPool (int numThreads) |
| Constructs a ThreadPool with a given number of worker threads to dispatch functions. More...
|
|
template<class F , class... Args> |
auto | enqueue (F &&f, Args &&...args) -> std::future< typename std::invoke_result< F, Args... >::type > |
| Enqueues a task to the thread pool. More...
|
|
| ~ThreadPool () |
|
- Examples
- factors.cpp, and primes.cpp.
Definition at line 60 of file threads.hpp.
◆ ThreadPool() [1/2]
gpmp::core::ThreadPool::ThreadPool |
( |
| ) |
|
|
inline |
Default constructor that creates a ThreadPool with the number of threads.
Definition at line 78 of file threads.hpp.
78 :
ThreadPool(std::thread::hardware_concurrency()) {
ThreadPool()
Default constructor that creates a ThreadPool with the number of threads.
◆ ThreadPool() [2/2]
gpmp::core::ThreadPool::ThreadPool |
( |
int |
numThreads | ) |
|
|
inlineexplicit |
Constructs a ThreadPool with a given number of worker threads to dispatch functions.
- Parameters
-
numThreads | The number of worker threads to be created. |
Constructs a ThreadPool object with the specified number of worker threads.
Definition at line 88 of file threads.hpp.
91 for (
int i = 0; i < numThreads; ++i) {
98 std::function<void()> task_obj;
104 std::unique_lock<std::mutex> lock(this->
queue_mutex);
109 return this->
stop || !this->
tasks.empty();
119 task_obj = std::move(this->
tasks.front());
std::condition_variable condition
std::vector< std::thread > workers
std::queue< std::function< void()> > tasks
References workers.
◆ ~ThreadPool()
gpmp::core::ThreadPool::~ThreadPool |
( |
| ) |
|
|
inline |
◆ enqueue()
template<class F , class... Args>
auto gpmp::core::ThreadPool::enqueue |
( |
F && |
f, |
|
|
Args &&... |
args |
|
) |
| -> std::future<typename std::invoke_result<F, Args...>::type> |
|
inline |
Enqueues a task to the thread pool.
- Template Parameters
-
F | Type of the function to be enqueued. |
Args | Variadic template parameter pack of the arguments passed to the function. |
- Parameters
-
f | Function to be enqueued. |
args | Arguments passed to the function |
- Returns
- std::future<typename std::result_of<F(Args...)>::type> A future object that will contain the result of the function execution.
- Exceptions
-
std::runtime_error | If the ThreadPool has already been stopped. |
- Examples
- primes.cpp.
Definition at line 148 of file threads.hpp.
154 using return_type =
typename std::invoke_result<F, Args...>::type;
162 auto task = std::make_shared<std::packaged_task<return_type()>>(
163 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
166 std::future<return_type>
res = task->get_future();
172 throw std::runtime_error(
"enqueue on stopped ThreadPool");
176 tasks.emplace([task]() { (*task)(); });
References condition, queue_mutex, python.linalg::res, stop, and tasks.
Referenced by testing_miller_thread().
◆ condition
std::condition_variable gpmp::core::ThreadPool::condition |
|
private |
◆ queue_mutex
std::mutex gpmp::core::ThreadPool::queue_mutex |
|
private |
◆ stop
bool gpmp::core::ThreadPool::stop |
|
private |
◆ tasks
std::queue<std::function<void()> > gpmp::core::ThreadPool::tasks |
|
private |
◆ workers
std::vector<std::thread> gpmp::core::ThreadPool::workers |
|
private |
The documentation for this class was generated from the following file: