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 21 of file thread_pool.cpp.
22 {
23 _workers.reserve(numThreads);
24 for (unsigned i = 0; i < numThreads; ++i)
25 {
26 _workers.emplace_back(
27 [this]
28 {
29 while (true)
30 {
31 Task task;
32 {
33 std::unique_lock lock(_queue_mutex);
34 _condition.wait(lock, [this] { return _stop || !_tasks.empty(); });
35 if (_stop && _tasks.empty())
36 {
37 return;
38 }
39 task = std::move(_tasks.front());
40 _tasks.pop();
41 }
42 try
43 {
44 task();
45 }
46 catch (const std::exception& e)
47 {
49 }
50
51 {
52 std::unique_lock lock(_queue_mutex);
53 --_pending_tasks;
54 if (_pending_tasks == 0)
55 {
56 _done_condition.notify_all();
57 }
58 }
59 }
60 });
61 }
62 }
@ ERROR
Error level for error events.
References logging::ERROR, and LOG.
◆ ~ThreadPool()
| pool::ThreadPool::~ThreadPool |
( |
| ) |
|
Destroys the ThreadPool, joining all threads.
Definition at line 64 of file thread_pool.cpp.
65 {
66 {
67 std::unique_lock lock(_queue_mutex);
68 _stop = true;
69 _condition.notify_all();
70 }
71 for (std::thread& worker : _workers)
72 {
73 worker.join();
74 }
75 }
◆ 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 77 of file thread_pool.cpp.
78 {
79 std::unique_lock lock(_queue_mutex);
80 const unsigned active_threads = _pending_tasks;
81 if (_workers.size() > static_cast<std::size_t>(std::numeric_limits<unsigned>::max()))
82 {
83 throw std::runtime_error("Thread pool size exceeds unsigned range.");
84 }
85 const unsigned total_threads = static_cast<unsigned>(_workers.size());
86 return total_threads > active_threads ? total_threads - active_threads : 0;
87 }
◆ 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: