Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2022 Euclid Science Ground Segment
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 3.0 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
25#ifndef _ALEXANDRIAKERNEL_THREADPOOL_H
26#define _ALEXANDRIAKERNEL_THREADPOOL_H
27
28#include <atomic>
29#include <deque>
30#include <exception>
31#include <functional>
32#include <mutex>
33#include <thread>
34#include <vector>
35
36namespace Euclid {
37
69
70public:
72 using Task = std::function<void(void)>;
73
82 explicit ThreadPool(unsigned int thread_count = std::thread::hardware_concurrency(),
83 unsigned int empty_queue_wait_time = 50);
84
87 virtual ~ThreadPool();
88
90 void submit(Task task);
91
94 void block(bool throw_on_exception = true);
95
97 bool checkForException(bool rethrow = false);
98
100 size_t queued() const;
101
103 size_t running() const;
104
106 size_t activeThreads() const;
107
108private:
117
118}; /* End of ThreadPool class */
119
120} /* namespace Euclid */
121
122#endif
Basic thread pool implementation.
Definition ThreadPool.h:68
void submit(Task task)
Submit a task to be executed.
std::deque< Task > m_queue
Definition ThreadPool.h:114
size_t running() const
Return the number of running tasks.
void block(bool throw_on_exception=true)
std::vector< std::atomic< bool > > m_worker_sleeping_flags
Definition ThreadPool.h:111
size_t queued() const
Return the number of queued tasks.
unsigned int m_empty_queue_wait_time
Definition ThreadPool.h:115
std::vector< std::thread > m_workers
Definition ThreadPool.h:113
std::mutex m_queue_mutex
Definition ThreadPool.h:109
std::vector< std::atomic< bool > > m_worker_run_flags
Definition ThreadPool.h:110
std::vector< std::atomic< bool > > m_worker_done_flags
Definition ThreadPool.h:112
bool checkForException(bool rethrow=false)
Checks if any task has thrown an exception and optionally rethrows it.
std::exception_ptr m_exception_ptr
Definition ThreadPool.h:116
size_t activeThreads() const
Return the number of active workers (either running or sleeping)
T hardware_concurrency(T... args)