Note that there are some explanatory texts on larger screens.

plurals
  1. POException thrown using boost thread library
    primarykey
    data
    text
    <p>I have the following code to run a timed thread:</p> <pre><code>// Method to invoke a request with a timeout. bool devices::server::CDeviceServer::invokeWithTimeout(CDeviceClientRequest&amp; request, CDeviceServerResponse&amp; response) { // Retrieve the timeout from the device. int timeout = getTimeout(); timeout += 500; // Add 500ms to cover invocation time. // Invoke the request within a timed thread. boost::promise&lt;void&gt; boostPromise; boost::unique_future&lt;void&gt; boostFuture = boostPromise.get_future(); boost::thread boostThread([&amp;]() { invoke(request, response); boostPromise.set_value(); }); // The thread has timed out, if the future is not ready. return (boostFuture.wait_for(boost::chrono::milliseconds(timeout)) == boost::future_status::ready); } </code></pre> <p>This appears to work without a problem, the function returns false when it times out.</p> <p>However, then the code being invoked (by invoke(request, response);) throws an exception which kills the application. How do I successfully terminate the thread if it has not completed, and consume any exceptions.</p> <p>I have tried the following:</p> <pre><code>// The thread has timed out, if the future is not ready. bool completed = (boostFuture.wait_for(boost::chrono::milliseconds(timeout)) == boost::future_status::ready); if (!completed) { boostThread.interrupt(); } return completed; </code></pre> <p>But this also throws an exception and crashes the application. I need a completely safe mechanism where the timed thread can be safely killed off, if the timeout has been reached.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload