Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pool.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_THREAD_POOL_HPP
2 #define OSMIUM_THREAD_POOL_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <algorithm>
37 #include <atomic>
38 #include <cstddef>
39 #include <cstdlib>
40 #include <future>
41 #include <thread>
42 #include <type_traits>
43 #include <vector>
44 
46 #include <osmium/thread/queue.hpp>
47 #include <osmium/thread/util.hpp>
48 #include <osmium/util/config.hpp>
49 
50 namespace osmium {
51 
55  namespace thread {
56 
60  class Pool {
61 
66  class thread_joiner {
67 
68  std::vector<std::thread>& m_threads;
69 
70  public:
71 
72  explicit thread_joiner(std::vector<std::thread>& threads) :
73  m_threads(threads) {
74  }
75 
77  for (auto& thread : m_threads) {
78  if (thread.joinable()) {
79  thread.join();
80  }
81  }
82  }
83 
84  }; // class thread_joiner
85 
86  std::atomic<bool> m_done;
88  std::vector<std::thread> m_threads;
91 
92  void worker_thread() {
93  osmium::thread::set_thread_name("_osmium_worker");
94  while (!m_done) {
95  function_wrapper task;
96  m_work_queue.wait_and_pop_with_timeout(task);
97  if (task) {
98  task();
99  }
100  }
101  }
102 
115  explicit Pool(int num_threads, size_t max_queue_size) :
116  m_done(false),
117  m_work_queue(max_queue_size, "work"),
118  m_threads(),
119  m_joiner(m_threads),
120  m_num_threads(num_threads) {
121 
122  if (m_num_threads == 0) {
123  m_num_threads = osmium::config::get_pool_threads();
124  }
125 
126  if (m_num_threads <= 0) {
127  m_num_threads = std::max(1, static_cast<int>(std::thread::hardware_concurrency()) + m_num_threads);
128  }
129 
130  try {
131  for (int i = 0; i < m_num_threads; ++i) {
132  m_threads.push_back(std::thread(&Pool::worker_thread, this));
133  }
134  } catch (...) {
135  m_done = true;
136  throw;
137  }
138  }
139 
140  public:
141 
142  static constexpr int default_num_threads = 0;
143  static constexpr size_t max_work_queue_size = 10;
144 
145  static Pool& instance() {
146  static Pool pool(default_num_threads, max_work_queue_size);
147  return pool;
148  }
149 
150  ~Pool() {
151  m_done = true;
152  m_work_queue.shutdown();
153  }
154 
155  size_t queue_size() const {
156  return m_work_queue.size();
157  }
158 
159  bool queue_empty() const {
160  return m_work_queue.empty();
161  }
162 
163  template <typename TFunction>
165 
166  typedef typename std::result_of<TFunction()>::type result_type;
167 
168  std::packaged_task<result_type()> task(std::forward<TFunction>(func));
169  std::future<result_type> future_result(task.get_future());
170  m_work_queue.push(std::move(task));
171 
172  return future_result;
173  }
174 
175  }; // class Pool
176 
177  } // namespace thread
178 
179 } // namespace osmium
180 
181 #endif // OSMIUM_THREAD_POOL_HPP
Pool(int num_threads, size_t max_queue_size)
Definition: pool.hpp:115
type
Definition: entity_bits.hpp:60
size_t size() const
Definition: queue.hpp:181
bool empty() const
Definition: queue.hpp:176
Definition: queue.hpp:56
~Pool()
Definition: pool.hpp:150
static constexpr size_t max_work_queue_size
Definition: pool.hpp:143
std::vector< std::thread > & m_threads
Definition: pool.hpp:68
void shutdown()
Definition: queue.hpp:137
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:164
static constexpr int default_num_threads
Definition: pool.hpp:142
void worker_thread()
Definition: pool.hpp:92
static Pool & instance()
Definition: pool.hpp:145
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
void wait_and_pop_with_timeout(T &value)
Definition: queue.hpp:153
std::atomic< bool > m_done
Definition: pool.hpp:86
int get_pool_threads()
Definition: config.hpp:47
Definition: function_wrapper.hpp:48
std::vector< std::thread > m_threads
Definition: pool.hpp:88
Definition: pool.hpp:60
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:87
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:72
int m_num_threads
Definition: pool.hpp:90
~thread_joiner()
Definition: pool.hpp:76
void push(T value)
Definition: queue.hpp:118
bool queue_empty() const
Definition: pool.hpp:159
void set_thread_name(const char *name)
Definition: util.hpp:74
thread_joiner m_joiner
Definition: pool.hpp:89
size_t queue_size() const
Definition: pool.hpp:155