Alexandria  2.18
Please provide a description of the 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(), unsigned int empty_queue_wait_time = 50);
83 
86  virtual ~ThreadPool();
87 
89  void submit(Task task);
90 
93  void block();
94 
96  bool checkForException(bool rethrow = false);
97 
99  size_t queued() const;
100 
102  size_t running() const;
103 
104 private:
113 
114 }; /* End of ThreadPool class */
115 
116 } /* namespace Euclid */
117 
118 #endif
std::exception_ptr
Euclid::ThreadPool::~ThreadPool
virtual ~ThreadPool()
Definition: ThreadPool.cpp:155
std::vector
STL class.
Euclid::ThreadPool::running
size_t running() const
Return the number of running tasks.
Definition: ThreadPool.cpp:132
Euclid::ThreadPool::ThreadPool
ThreadPool(unsigned int thread_count=std::thread::hardware_concurrency(), unsigned int empty_queue_wait_time=50)
Constructs a new ThreadPool.
Definition: ThreadPool.cpp:89
Euclid::ThreadPool::m_worker_run_flags
std::vector< std::atomic< bool > > m_worker_run_flags
Definition: ThreadPool.h:106
Euclid::ThreadPool::m_worker_done_flags
std::vector< std::atomic< bool > > m_worker_done_flags
Definition: ThreadPool.h:108
std::function
Euclid::ThreadPool::m_queue
std::deque< Task > m_queue
Definition: ThreadPool.h:110
Euclid::ThreadPool::m_workers
std::vector< std::thread > m_workers
Definition: ThreadPool.h:109
std::thread::hardware_concurrency
T hardware_concurrency(T... args)
Euclid::ThreadPool::queued
size_t queued() const
Return the number of queued tasks.
Definition: ThreadPool.cpp:127
Euclid::ThreadPool::checkForException
bool checkForException(bool rethrow=false)
Checks if any task has thrown an exception and optionally rethrows it.
Definition: ThreadPool.cpp:116
std::deque
STL class.
Euclid::ThreadPool::m_empty_queue_wait_time
unsigned int m_empty_queue_wait_time
Definition: ThreadPool.h:111
Euclid::ThreadPool::block
void block()
Definition: ThreadPool.cpp:138
Euclid::ThreadPool
Basic thread pool implementation.
Definition: ThreadPool.h:68
std::mutex
STL class.
Euclid::ThreadPool::m_queue_mutex
std::mutex m_queue_mutex
Definition: ThreadPool.h:105
Euclid::ThreadPool::m_worker_sleeping_flags
std::vector< std::atomic< bool > > m_worker_sleeping_flags
Definition: ThreadPool.h:107
Euclid::ThreadPool::m_exception_ptr
std::exception_ptr m_exception_ptr
Definition: ThreadPool.h:112
Euclid::ThreadPool::submit
void submit(Task task)
Submit a task to be executed.
Definition: ThreadPool.cpp:166
Euclid
Definition: InstOrRefHolder.h:29