1 #ifndef OSMIUM_THREAD_POOL_HPP 2 #define OSMIUM_THREAD_POOL_HPP 39 #include <type_traits> 59 constexpr
const int max_pool_threads = 256;
61 inline int get_pool_size(
int num_threads,
int user_setting,
unsigned hardware_concurrency) {
62 if (num_threads == 0) {
63 num_threads = user_setting ? user_setting : -2;
66 if (num_threads < 0) {
67 num_threads += int(hardware_concurrency);
70 if (num_threads < 1) {
72 }
else if (num_threads > max_pool_threads) {
73 num_threads = max_pool_threads;
79 inline std::size_t get_work_queue_size() noexcept {
106 for (
auto& thread : m_threads) {
107 if (thread.joinable()) {
125 if (task && task()) {
135 static constexpr
int default_num_threads = 0;
136 static constexpr
int default_queue_size = 0;
153 explicit Pool(
int num_threads = default_num_threads, std::size_t max_queue_size = default_queue_size) :
154 m_work_queue(max_queue_size > 0 ? max_queue_size :
detail::get_work_queue_size(),
"work"),
160 for (
int i = 0; i < m_num_threads; ++i) {
164 shutdown_all_workers();
175 for (
int i = 0; i < m_num_threads; ++i) {
182 shutdown_all_workers();
186 return m_num_threads;
190 return m_work_queue.
size();
194 return m_work_queue.
empty();
197 template <
typename TFunction>
201 std::packaged_task<result_type()> task{std::forward<TFunction>(func)};
202 std::future<result_type> future_result{task.get_future()};
203 m_work_queue.
push(std::move(task));
205 return future_result;
214 #endif // OSMIUM_THREAD_POOL_HPP type
Definition: entity_bits.hpp:63
std::size_t get_max_queue_size(const char *queue_name, std::size_t default_value) noexcept
Definition: config.hpp:71
static Pool & default_instance()
Definition: pool.hpp:169
bool queue_empty() const
Definition: pool.hpp:193
void set_thread_name(const char *name) noexcept
Definition: util.hpp:76
~Pool()
Definition: pool.hpp:181
Definition: reader_iterator.hpp:39
std::size_t queue_size() const
Definition: pool.hpp:189
std::vector< std::thread > & m_threads
Definition: pool.hpp:97
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:198
void worker_thread()
Definition: pool.hpp:120
int get_pool_threads() noexcept
Definition: config.hpp:50
int num_threads() const noexcept
Definition: pool.hpp:185
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: function_wrapper.hpp:48
std::vector< std::thread > m_threads
Definition: pool.hpp:116
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:115
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:101
int m_num_threads
Definition: pool.hpp:118
void wait_and_pop(T &value)
Definition: queue.hpp:169
~thread_joiner()
Definition: pool.hpp:105
void push(T value)
Definition: queue.hpp:144
bool empty() const
Definition: queue.hpp:213
void shutdown_all_workers()
Definition: pool.hpp:174
thread_joiner m_joiner
Definition: pool.hpp:117
std::size_t size() const
Definition: queue.hpp:218
Pool(int num_threads=default_num_threads, std::size_t max_queue_size=default_queue_size)
Definition: pool.hpp:153