From 3ac6e95ead1e219ab38cf9dd7a886f5275fcc94c Mon Sep 17 00:00:00 2001 From: Maxim Gordeev Date: Mon, 21 Mar 2022 19:30:30 +0300 Subject: [PATCH] [IE Samples] Fixed hanging of samples if InferImpl() throws exception (#11075) * [IE Samples] Fixed hanging of samples if InferImpl() throws exception * improved re-throw an exception --- samples/cpp/classification_sample_async/main.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/samples/cpp/classification_sample_async/main.cpp b/samples/cpp/classification_sample_async/main.cpp index fe2684f273d..aa3ab377766 100644 --- a/samples/cpp/classification_sample_async/main.cpp +++ b/samples/cpp/classification_sample_async/main.cpp @@ -166,11 +166,14 @@ int main(int argc, char* argv[]) { size_t cur_iteration = 0; std::condition_variable condVar; std::mutex mutex; - + std::exception_ptr exception_var; // -------- Step 10. Do asynchronous inference -------- infer_request.set_callback([&](std::exception_ptr ex) { - if (ex) - throw ex; + if (ex) { + exception_var = ex; + condVar.notify_all(); + return; + } std::lock_guard l(mutex); cur_iteration++; @@ -193,6 +196,10 @@ int main(int argc, char* argv[]) { // Wait all iterations of the async request std::unique_lock lock(mutex); condVar.wait(lock, [&] { + if (exception_var) { + std::rethrow_exception(exception_var); + } + return cur_iteration == num_iterations; });