Skip to content

Commit 051eca2

Browse files
committed
Refactor do_jobs
1 parent e733b90 commit 051eca2

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

src/osmdata.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,30 +168,35 @@ struct pending_threaded_processor : public pending_processor
168168
using output_vec_t = std::vector<std::shared_ptr<output_t>>;
169169
using pending_queue_t = std::stack<osmid_t>;
170170

171-
static void do_jobs(output_vec_t const &outputs, pending_queue_t &queue,
172-
std::mutex &mutex, bool ways)
171+
static osmid_t pop_id(pending_queue_t &queue, std::mutex &mutex)
173172
{
174-
while (true) {
175-
//get the job off the queue synchronously
176-
osmid_t job;
177-
mutex.lock();
178-
if (queue.empty()) {
179-
mutex.unlock();
180-
break;
181-
}
182-
job = queue.top();
173+
osmid_t id = 0;
174+
175+
std::lock_guard<std::mutex> const lock{mutex};
176+
if (!queue.empty()) {
177+
id = queue.top();
183178
queue.pop();
184-
mutex.unlock();
179+
}
185180

186-
//process it
187-
if (ways) {
188-
for (auto const &out : outputs) {
189-
out->pending_way(job);
190-
}
191-
} else {
192-
for (auto const &out : outputs) {
193-
out->pending_relation(job);
194-
}
181+
return id;
182+
}
183+
184+
static void do_ways(output_vec_t const &outputs, pending_queue_t &queue,
185+
std::mutex &mutex)
186+
{
187+
while (osmid_t const id = pop_id(queue, mutex)) {
188+
for (auto const &output : outputs) {
189+
output->pending_way(id);
190+
}
191+
}
192+
}
193+
194+
static void do_rels(output_vec_t const &outputs, pending_queue_t &queue,
195+
std::mutex &mutex)
196+
{
197+
while (osmid_t const id = pop_id(queue, mutex)) {
198+
for (auto const &output : outputs) {
199+
output->pending_relation(id);
195200
}
196201
}
197202
}
@@ -251,9 +256,9 @@ struct pending_threaded_processor : public pending_processor
251256
//make the threads and start them
252257
std::vector<std::future<void>> workers;
253258
for (auto const &clone : m_clones) {
254-
workers.push_back(std::async(std::launch::async, do_jobs,
259+
workers.push_back(std::async(std::launch::async, do_ways,
255260
std::cref(clone), std::ref(queue),
256-
std::ref(mutex), true));
261+
std::ref(mutex)));
257262
}
258263
workers.push_back(std::async(std::launch::async, print_stats,
259264
std::ref(queue), std::ref(mutex)));
@@ -300,9 +305,9 @@ struct pending_threaded_processor : public pending_processor
300305
//make the threads and start them
301306
std::vector<std::future<void>> workers;
302307
for (auto const &clone : m_clones) {
303-
workers.push_back(std::async(std::launch::async, do_jobs,
308+
workers.push_back(std::async(std::launch::async, do_rels,
304309
std::cref(clone), std::ref(queue),
305-
std::ref(mutex), false));
310+
std::ref(mutex)));
306311
}
307312
workers.push_back(std::async(std::launch::async, print_stats,
308313
std::ref(queue), std::ref(mutex)));

0 commit comments

Comments
 (0)