Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no (standard) way to reach into a thread and kill it, and this is generally a bad idea anyway. The much cleaner option is to pass a starting time and max duration to the function and then (possibly multiple times as the calculation progresses) check if the current time minus the starting time is too long.</p> <p>I would make something like this:</p> <pre><code>#include &lt;chrono&gt; template &lt;typename Clock = std::chrono::steady_clock&gt; class timeout { public: typedef Clock clock_type; typedef typename clock_type::time_point time_point; typedef typename clock_type::duration duration; explicit timeout(duration maxDuration) : mStartTime(clock_type::now()), mMaxDuration(maxDuration) {} time_point start_time() const { return mStartTime; } duration max_duration() const { return mMaxDuration; } bool is_expired() const { const auto endTime = clock_type::now(); return (endTime - start_time()) &gt; max_duration(); } static timeout infinity() { return timeout(duration::max()); } private: time_point mStartTime; duration mMaxDuration; }; </code></pre> <p>This simple utility tracks the starting time and a max duration (and provides a way of specifying infinity), and allows the user to query simple facts, most importantly whether or not a timeout has occurred.</p> <p>Test below; you can add fake delay by defining/undefining <code>FAKE_DELAY</code>:</p> <pre><code>#include &lt;iostream&gt; #include &lt;future&gt; #define FAKE_DELAY void fake_delay() { #ifdef FAKE_DELAY std::this_thread::sleep_for(std::chrono::milliseconds(1000)); #endif } void short_running_function(timeout&lt;&gt; timelimit) { fake_delay(); if (timelimit.is_expired()) std::cout &lt;&lt; "short running thread ran out of time" &lt;&lt; std::endl; else std::cout &lt;&lt; "short running function finished" &lt;&lt; std::endl; } void long_running_function(timeout&lt;&gt; timelimit) { for (unsigned i = 0; i &lt; 10; ++i) { if (timelimit.is_expired()) { std::cout &lt;&lt; "long running thread ran out of time" &lt;&lt; std::endl; return; } std::cout &lt;&lt; "long running thread doing work" &lt;&lt; std::endl; fake_delay(); } std::cout &lt;&lt; "long running function finished" &lt;&lt; std::endl; } int main() { std::async(short_running_function, timeout&lt;&gt;(std::chrono::milliseconds(500))).wait(); std::async(short_running_function, timeout&lt;&gt;(std::chrono::milliseconds(5000))).wait(); std::async(long_running_function, timeout&lt;&gt;(std::chrono::milliseconds(500))).wait(); std::async(long_running_function, timeout&lt;&gt;(std::chrono::milliseconds(5000))).wait(); std::async(long_running_function, timeout&lt;&gt;::infinity()).wait(); } </code></pre> <p>One possible output with <code>FAKE_DELAY</code> <strong>off</strong>:</p> <blockquote> <p>short running function finished<br> short running function finished<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running function finished<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running function finished<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running function finished </p> </blockquote> <p>One possible output with <code>FAKE_DELAY</code> <strong>on</strong>:</p> <blockquote> <p>short running thread ran out of time<br> short running function finished<br> long running thread doing work<br> long running thread ran out of time<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread ran out of time<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running thread doing work<br> long running function finished </p> </blockquote>
 

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