1 #ifndef OSMIUM_THREAD_POOL_HPP 2 #define OSMIUM_THREAD_POOL_HPP 42 #include <type_traits> 61 constexpr
const int max_pool_threads = 256;
63 inline int get_pool_size(
int num_threads,
int user_setting,
unsigned hardware_concurrency) {
64 if (num_threads == 0) {
65 num_threads = user_setting ? user_setting : -2;
68 if (num_threads < 0) {
69 num_threads += int(hardware_concurrency);
72 if (num_threads < 1) {
74 }
else if (num_threads > max_pool_threads) {
75 num_threads = max_pool_threads;
81 inline size_t get_work_queue_size() noexcept {
108 for (
auto& thread : m_threads) {
109 if (thread.joinable()) {
127 if (task && task()) {
147 explicit Pool(
int num_threads,
size_t max_queue_size) :
148 m_work_queue(max_queue_size,
"work"),
154 for (
int i = 0; i < m_num_threads; ++i) {
158 shutdown_all_workers();
165 static constexpr
int default_num_threads = 0;
168 static Pool pool(default_num_threads, detail::get_work_queue_size());
173 for (
int i = 0; i < m_num_threads; ++i) {
180 shutdown_all_workers();
184 return m_work_queue.
size();
188 return m_work_queue.
empty();
191 template <
typename TFunction>
196 std::packaged_task<result_type()> task(std::forward<TFunction>(func));
197 std::future<result_type> future_result(task.get_future());
198 m_work_queue.
push(std::move(task));
200 return future_result;
209 #endif // OSMIUM_THREAD_POOL_HPP Pool(int num_threads, size_t max_queue_size)
Definition: pool.hpp:147
type
Definition: entity_bits.hpp:63
size_t size() const
Definition: queue.hpp:196
bool empty() const
Definition: queue.hpp:191
void set_thread_name(const char *name) noexcept
Definition: util.hpp:74
~Pool()
Definition: pool.hpp:179
Definition: reader_iterator.hpp:39
std::vector< std::thread > & m_threads
Definition: pool.hpp:99
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:192
void worker_thread()
Definition: pool.hpp:122
int get_pool_threads() noexcept
Definition: config.hpp:48
static Pool & instance()
Definition: pool.hpp:167
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Definition: function_wrapper.hpp:48
std::vector< std::thread > m_threads
Definition: pool.hpp:118
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:117
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:103
int m_num_threads
Definition: pool.hpp:120
void wait_and_pop(T &value)
Definition: queue.hpp:156
~thread_joiner()
Definition: pool.hpp:107
void push(T value)
Definition: queue.hpp:134
size_t get_max_queue_size(const char *queue_name, size_t default_value) noexcept
Definition: config.hpp:69
void shutdown_all_workers()
Definition: pool.hpp:172
bool queue_empty() const
Definition: pool.hpp:187
thread_joiner m_joiner
Definition: pool.hpp:119
size_t queue_size() const
Definition: pool.hpp:183