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 += hardware_concurrency;
72 if (num_threads < 1) {
74 }
else if (num_threads > max_pool_threads) {
75 num_threads = max_pool_threads;
103 for (
auto& thread : m_threads) {
104 if (thread.joinable()) {
144 explicit Pool(
int num_threads,
size_t max_queue_size) :
145 m_work_queue(max_queue_size,
"work"),
166 static Pool pool(default_num_threads, max_work_queue_size);
183 return m_work_queue.
size();
187 return m_work_queue.
empty();
190 template <
typename TFunction>
195 std::packaged_task<result_type()> task(std::forward<TFunction>(func));
196 std::future<result_type> future_result(task.get_future());
197 m_work_queue.
push(std::move(task));
199 return future_result;
208 #endif // OSMIUM_THREAD_POOL_HPP
Pool(int num_threads, size_t max_queue_size)
Definition: pool.hpp:144
type
Definition: entity_bits.hpp:60
size_t size() const
Definition: queue.hpp:188
bool empty() const
Definition: queue.hpp:183
void set_thread_name(const char *name) noexcept
Definition: util.hpp:74
~Pool()
Definition: pool.hpp:177
Definition: reader_iterator.hpp:39
static constexpr size_t max_work_queue_size
Definition: pool.hpp:163
std::vector< std::thread > & m_threads
Definition: pool.hpp:94
void shutdown()
Definition: queue.hpp:144
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:191
static constexpr int default_num_threads
Definition: pool.hpp:162
void worker_thread()
Definition: pool.hpp:117
static Pool & instance()
Definition: pool.hpp:165
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
void wait_and_pop_with_timeout(T &value)
Definition: queue.hpp:160
int get_pool_threads()
Definition: config.hpp:47
Definition: function_wrapper.hpp:48
std::vector< std::thread > m_threads
Definition: pool.hpp:113
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:112
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:98
int m_num_threads
Definition: pool.hpp:115
~thread_joiner()
Definition: pool.hpp:102
void push(T value)
Definition: queue.hpp:122
void shutdown_all_workers()
Definition: pool.hpp:170
bool queue_empty() const
Definition: pool.hpp:186
thread_joiner m_joiner
Definition: pool.hpp:114
size_t queue_size() const
Definition: pool.hpp:182