0

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

Paul Childs
  • 229
  • 2
  • 9
  • Can you explain what you mean by writing *the task cause the thread to crash* ? Does it mean that `Method` throws exception? – rafix07 Aug 13 '19 at 07:10
  • I'm not too sure but I don't think so as any unhandled exceptions would be handled nicely by the code and I get test cases where it does. – Paul Childs Aug 14 '19 at 03:25

0 Answers0