I want to detect if a packaged task successfully completes, but have a few edge cases when errors in the task cause the thread to crash (from some 3rd party codes). In these cases no exceptions are caught by the below code and I am unable to tell that the thread crashed. The future_status is set as ready upon crash; however, if I try to get the result with .get() before I join the thread, the future will abort after internally calling _M_state->wait. Internally I can see that the future's _M_state._M_result is filled with garbage.
Can anyone think of how to determine if the task has successfully completed?
bool success = true;
try
{
std::packaged_task<int()> pTask(std::bind(&Method, pointerToObject, stuff));
std::future<int> ftr = pTask.get_future();
std::thread processingThread(std::move(pTask));
std::future_status status;
do
{
if (!ftr.valid()) throw std::future_error(std::future_errc::no_state);
status = ftr.wait_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
} while (status != std::future_status::ready);
processingThread.join();
}
catch (std::future_error const&) success=false;
catch (std::exception const&) success=false;
end