26 _workers.emplace_back(
33 std::unique_lock
lock(_queue_mutex);
34 _condition.wait(
lock, [
this] {
return _stop || !_tasks.empty(); });
35 if (_stop && _tasks.empty())
39 task = std::move(_tasks.front());
46 catch (
const std::exception&
e)
52 std::unique_lock
const lock(_queue_mutex);
54 if (_pending_tasks == 0)
56 _done_condition.notify_all();
67 std::unique_lock
const lock(_queue_mutex);
69 _condition.notify_all();
71 for (std::thread&
worker : _workers)
79 std::unique_lock
const lock(_queue_mutex);
81 if (_workers.size() >
static_cast<std::size_t
>(std::numeric_limits<unsigned>::max()))
83 throw std::runtime_error(
"Thread pool size exceeds unsigned range.");
85 const auto total_threads =
static_cast<unsigned>(_workers.size());
~ThreadPool()
Destroys the ThreadPool, joining all threads.
unsigned getAvailableThreads()
Returns the number of threads available for executing tasks.
ThreadPool(unsigned numThreads)
Constructs a ThreadPool with a specified number of threads.
Header file for the logging system.
@ ERROR
Error level for error events.
A simple thread pool implementation.