Alexandria  2.16
Please provide a description of the project.
ThreadPool.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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 <vector>
29 #include <thread>
30 #include <mutex>
31 #include <atomic>
32 #include <deque>
33 #include <functional>
34 #include <exception>
35 
36 namespace Euclid {
37 
68 class ThreadPool {
69 
70 public:
71 
73  using Task = std::function<void(void)>;
74 
83  explicit ThreadPool(unsigned int thread_count=std::thread::hardware_concurrency(), 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 
99 private:
100 
108 
109 }; /* End of ThreadPool class */
110 
111 } /* namespace Euclid */
112 
113 
114 #endif
std::exception_ptr
Euclid::ThreadPool::~ThreadPool
virtual ~ThreadPool()
Definition: ThreadPool.cpp:143
std::vector
STL class.
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:102
Euclid::ThreadPool::m_worker_done_flags
std::vector< std::atomic< bool > > m_worker_done_flags
Definition: ThreadPool.h:104
std::function
Euclid::ThreadPool::m_queue
std::deque< Task > m_queue
Definition: ThreadPool.h:105
std::thread::hardware_concurrency
T hardware_concurrency(T... args)
Euclid::ThreadPool::checkForException
bool checkForException(bool rethrow=false)
Checks if any task has thrown an exception and optionally rethrows it.
Definition: ThreadPool.cpp:114
std::deque
STL class.
Euclid::ThreadPool::m_empty_queue_wait_time
unsigned int m_empty_queue_wait_time
Definition: ThreadPool.h:106
Euclid::ThreadPool::block
void block()
Definition: ThreadPool.cpp:125
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:101
Euclid::ThreadPool::m_worker_sleeping_flags
std::vector< std::atomic< bool > > m_worker_sleeping_flags
Definition: ThreadPool.h:103
Euclid::ThreadPool::m_exception_ptr
std::exception_ptr m_exception_ptr
Definition: ThreadPool.h:107
Euclid::ThreadPool::submit
void submit(Task task)
Submit a task to be executed.
Definition: ThreadPool.cpp:153
Euclid
Definition: InstOrRefHolder.h:29