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; });