Alexandria  2.25.0
SDC-CH common library for the Euclid project
ThreadPool.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 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 
36 namespace Euclid {
37 
68 class ThreadPool {
69 
70 public:
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();
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 
108 private:
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.
Definition: ThreadPool.cpp:172
std::deque< Task > m_queue
Definition: ThreadPool.h:114
size_t running() const
Return the number of running tasks.
Definition: ThreadPool.cpp:133
std::vector< std::atomic< bool > > m_worker_sleeping_flags
Definition: ThreadPool.h:111
size_t queued() const
Return the number of queued tasks.
Definition: ThreadPool.cpp:128
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.
Definition: ThreadPool.cpp:117
virtual ~ThreadPool()
Definition: ThreadPool.cpp:161
std::exception_ptr m_exception_ptr
Definition: ThreadPool.h:116
size_t activeThreads() const
Return the number of active workers (either running or sleeping)
Definition: ThreadPool.cpp:139
ThreadPool(unsigned int thread_count=std::thread::hardware_concurrency(), unsigned int empty_queue_wait_time=50)
Constructs a new ThreadPool.
Definition: ThreadPool.cpp:90
T hardware_concurrency(T... args)