Skip to content

Commit 2632899

Browse files
committed
try graceful exit in parallel_for_dynamic
1 parent 9d6741c commit 2632899

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/support/parallel_for.cc

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,36 @@ void parallel_for_dynamic(int begin, int end, int num_threads,
118118
futures.emplace_back(task.get_future());
119119
threads.emplace_back(std::move(task), thread_id);
120120
}
121+
121122
// Step 2.2. Launch worker 0 inplace
123+
124+
auto try_await_futures = [&futures]() {
125+
try {
126+
for (auto&& future : futures) {
127+
future.get();
128+
}
129+
} catch (const std::exception& e) {
130+
LOG(WARNING) << "RuntimeError: parallel_for_dynamic error on future get with " << e.what();
131+
}
132+
};
133+
122134
try {
123135
worker(0);
124136
} catch (const std::exception& e) {
125137
for (auto&& thread : threads) {
126138
thread.join();
127139
}
128-
LOG(FATAL) << "RuntimeError: parallel_for_dynamic error with " << e.what();
140+
LOG(WARNING) << "RuntimeError: parallel_for_dynamic error on thread join with " << e.what();
141+
try_await_futures();
142+
return;
129143
}
144+
130145
// Step 3. Join threads and check exceptions
131146
for (auto&& thread : threads) {
132147
thread.join();
133148
}
134-
try {
135-
for (auto&& future : futures) {
136-
future.get();
137-
}
138-
} catch (const std::exception& e) {
139-
LOG(FATAL) << "RuntimeError: parallel_for_dynamic error with " << e.what();
140-
}
149+
150+
try_await_futures();
141151
}
142152

143153
} // namespace support

0 commit comments

Comments
 (0)