1 // Copyright (C) 2019-2020 Joel Rosdahl and other contributors
3 // See doc/AUTHORS.adoc for a complete list of contributors.
5 // This program is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3 of the License, or (at your option)
10 // This program is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 // You should have received a copy of the GNU General Public License along with
16 // this program; if not, write to the Free Software Foundation, Inc., 51
17 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "ThreadPool.hpp"
21 ThreadPool::ThreadPool(size_t number_of_threads, size_t task_queue_max_size)
22 : m_task_queue_max_size(task_queue_max_size)
24 m_worker_threads.reserve(number_of_threads);
25 for (size_t i = 0; i < number_of_threads; ++i) {
26 m_worker_threads.emplace_back(&ThreadPool::worker_thread_main, this);
30 ThreadPool::~ThreadPool()
36 ThreadPool::enqueue(std::function<void()> function)
39 std::unique_lock<std::mutex> lock(m_mutex);
40 if (m_task_queue.size() >= m_task_queue_max_size) {
41 m_task_popped_condition.wait(
42 lock, [this] { return m_task_queue.size() < m_task_queue_max_size; });
44 m_task_queue.emplace(function);
46 m_task_enqueued_or_shutting_down_condition.notify_one();
50 ThreadPool::shut_down()
53 std::unique_lock<std::mutex> lock(m_mutex);
54 m_shutting_down = true;
56 m_task_enqueued_or_shutting_down_condition.notify_all();
57 for (auto& thread : m_worker_threads) {
58 if (thread.joinable()) {
65 ThreadPool::worker_thread_main()
68 std::function<void()> task;
71 std::unique_lock<std::mutex> lock(m_mutex);
72 m_task_enqueued_or_shutting_down_condition.wait(
73 lock, [this] { return m_shutting_down || !m_task_queue.empty(); });
74 if (m_shutting_down && m_task_queue.empty()) {
77 task = std::move(m_task_queue.front());
81 m_task_popped_condition.notify_all();