A simple thread pool implementation.
More...
#include "thread_pool.h"
A simple thread pool implementation.
Definition at line 28 of file thread_pool.h.
◆ ThreadPool() [1/3]
| pool::ThreadPool::ThreadPool |
( |
unsigned |
numThreads | ) |
|
|
explicit |
Constructs a ThreadPool with a specified number of threads.
- Parameters
-
| numThreads | The number of threads in the pool. |
Definition at line 18 of file thread_pool.cpp.
19 {
20 _workers.reserve(numThreads);
21 for (unsigned i = 0; i < numThreads; ++i)
22 {
23 _workers.emplace_back(
24 [this]
25 {
26 while (true)
27 {
28 Task task;
29 {
30 std::unique_lock lock(_queue_mutex);
31 _condition.wait(lock, [this] { return _stop || !_tasks.empty(); });
32 if (_stop && _tasks.empty())
33 {
34 return;
35 }
36 task = std::move(_tasks.front());
37 _tasks.pop();
38 }
39 try
40 {
41 task();
42 }
43 catch (const std::exception& e)
44 {
46 }
47
48 {
49 std::unique_lock lock(_queue_mutex);
50 --_pending_tasks;
51 if (_pending_tasks == 0)
52 {
53 _done_condition.notify_all();
54 }
55 }
56 }
57 });
58 }
59 }
@ ERROR
Error level for error events.
References logging::ERROR, and LOG.
◆ ~ThreadPool()
| pool::ThreadPool::~ThreadPool |
( |
| ) |
|
Destroys the ThreadPool, joining all threads.
Definition at line 61 of file thread_pool.cpp.
62 {
63 {
64 std::unique_lock lock(_queue_mutex);
65 _stop = true;
66 _condition.notify_all();
67 }
68 for (std::thread& worker : _workers)
69 {
70 worker.join();
71 }
72 }
◆ ThreadPool() [2/3]
| pool::ThreadPool::ThreadPool |
( |
const ThreadPool & |
| ) |
|
|
delete |
◆ ThreadPool() [3/3]
◆ enqueue()
template<class F , class... Args>
| std::future< std::invoke_result_t< F, Args... > > pool::ThreadPool::enqueue |
( |
F && |
f, |
|
|
Args &&... |
args |
|
) |
| |
Enqueues a task to be executed by the thread pool.
- Template Parameters
-
| F | The type of the function to be executed. |
| Args | The types of the arguments to the function. |
- Parameters
-
| f | The function to be executed. |
| args | The arguments to the function. |
- Returns
- A future that will hold the result of the function.
- Exceptions
-
| std::runtime_error | if the thread pool is stopped. |
Definition at line 57 of file thread_pool.h.
58 {
59 using ReturnType = std::invoke_result_t<F, Args...>;
60
61 auto task = std::make_shared<std::packaged_task<ReturnType()>>(
62 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
63
64 std::future<ReturnType> res = task->get_future();
65 {
66 std::unique_lock lock(_queue_mutex);
67 if (_stop)
68 {
69 throw std::runtime_error("enqueue on stopped ThreadPool");
70 }
71
72 ++_pending_tasks;
73 _tasks.emplace([task] { (*task)(); });
74 _condition.notify_one();
75 }
76 return res;
77 }
◆ getAvailableThreads()
| unsigned pool::ThreadPool::getAvailableThreads |
( |
| ) |
|
Returns the number of threads available for executing tasks.
- Returns
- The number of available threads.
Definition at line 74 of file thread_pool.cpp.
75 {
76 std::unique_lock lock(_queue_mutex);
77 const unsigned active_threads = _pending_tasks;
78 const unsigned total_threads = _workers.size();
79 return total_threads > active_threads ? total_threads - active_threads : 0;
80 }
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ wait()
| void pool::ThreadPool::wait |
( |
| ) |
|
Waits for all tasks in the thread pool to finish.
Definition at line 82 of file thread_pool.h.
83 {
84 std::unique_lock lock(_queue_mutex);
85 _done_condition.wait(lock, [this] { return _pending_tasks == 0; });
86 }
The documentation for this class was generated from the following files: