Boost C++ Libraries: Ticket #9654: try_join_for(0) returns false when thread has exited https://svn.boost.org/trac10/ticket/9654 <p> I use try_join_for(boost::chrono::milliseconds(0)) to check if my thread has finished. </p> <p> When upgrading from boost 53 to boost 55 I noticed that it now returns false systematically. It seems to work fine if I use any duration other than 0. </p> <p> Here is a minimal code that reproduces the problem, I work with visual studio 2012 x64: </p> <pre class="wiki">#include &lt;boost\thread.hpp&gt; #include &lt;iostream&gt; void my_func(void){ std::cout&lt;&lt;"This shouldn't take long...\n"; } void main(void) { boost::thread * t = new boost::thread(my_func); int i = 0; while (! t-&gt;try_join_for(boost::chrono::milliseconds(1))) { std::cout&lt;&lt;"iteration "&lt;&lt;++i&lt;&lt;'\n'; boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } std::cout &lt;&lt; "first task finished\n"; delete t; t = new boost::thread(my_func); i=0; while (! t-&gt;try_join_for(boost::chrono::milliseconds(0))) { std::cout&lt;&lt;"iteration "&lt;&lt;++i&lt;&lt;'\n'; boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } std::cout &lt;&lt; "second task finished\n"; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9654 Trac 1.4.3 ernest.galbrun@… Thu, 13 Feb 2014 08:59:03 GMT <link>https://svn.boost.org/trac10/ticket/9654#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9654#comment:1</guid> <description> <p> Apparently it is caused by a previous bug fix: <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/8323"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/8323</a> </p> <p> Now try_join_for returns false immediatly if the current time is &gt;= to the finish time. </p> <p> It should, I think, check at least once if the thread has completed before returning false. I'm not sure how to do that, that's the point of calling try_join_for(0). </p> <p> Is there a reason why there is no try_join() function ? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 13 Feb 2014 18:31:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9654#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9654#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9654#comment:1" title="Comment 1">ernest.galbrun@…</a>: </p> <blockquote class="citation"> <p> Apparently it is caused by a previous bug fix: <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/8323"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/8323</a> </p> <p> Now try_join_for returns false immediately if the current time is &gt;= to the finish time. </p> </blockquote> <p> Right. </p> <blockquote class="citation"> <p> It should, I think, check at least once if the thread has completed before returning false. </p> </blockquote> <p> Well, this is not what the documentation says :( </p> <blockquote class="citation"> <p> I'm not sure how to do that, that's the point of calling try_join_for(0). </p> </blockquote> <p> Have you tried try_join_for(nanoseconds(1))? </p> <blockquote class="citation"> <p> Is there a reason why there is no try_join() function ? </p> </blockquote> <p> I don't know of a portable way to implement it. </p> <p> Please see <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9618" title="#9618: Bugs: try_join_for problem: program is not terminate. (closed: fixed)">#9618</a> and if you are on Windows, please could you check if the proposed patch works for you. </p> </description> <category>Ticket</category> </item> <item> <author>ernest.galbrun@…</author> <pubDate>Fri, 14 Feb 2014 09:18:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9654#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9654#comment:3</guid> <description> <p> Hi viboes, thanks for your reply I had not seen the previous issue. </p> <p> The patch works fine, thank you. </p> <p> I think the documentation should be more explicit wether this is allowed or not, since this is the prefered way of checking if a thread has finished when searching on stackoverflow: <a class="ext-link" href="http://stackoverflow.com/questions/1667420/how-can-i-tell-reliably-if-a-boost-thread-has-exited-its-run-method"><span class="icon">​</span>http://stackoverflow.com/questions/1667420/how-can-i-tell-reliably-if-a-boost-thread-has-exited-its-run-method</a> </p> <p> Maybe we should change the SO answer, what is your stance about this issue ? What is the <em>correct</em> way of checking wether a thread has finished? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 16 Feb 2014 09:45:12 GMT</pubDate> <title>status changed; resolution set; milestone deleted https://svn.boost.org/trac10/ticket/9654#comment:4 https://svn.boost.org/trac10/ticket/9654#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">duplicate</span> </li> <li><strong>milestone</strong> <span class="trac-field-deleted">To Be Determined</span> </li> </ul> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9654#comment:3" title="Comment 3">ernest.galbrun@…</a>: </p> <blockquote class="citation"> <p> Hi viboes, thanks for your reply I had not seen the previous issue. </p> <p> The patch works fine, thank you. </p> <p> I think the documentation should be more explicit wether this is allowed or not, since this is the prefered way of checking if a thread has finished when searching on stackoverflow: <a class="ext-link" href="http://stackoverflow.com/questions/1667420/how-can-i-tell-reliably-if-a-boost-thread-has-exited-its-run-method"><span class="icon">​</span>http://stackoverflow.com/questions/1667420/how-can-i-tell-reliably-if-a-boost-thread-has-exited-its-run-method</a> </p> <p> Maybe we should change the SO answer, what is your stance about this issue ? What is the <em>correct</em> way of checking whether a thread has finished? </p> </blockquote> <p> The answer of MSalters is the correct one. </p> <p> <em>You fundamentally can't do this. The reason is that the two possible answers are "Yes" and "Not when I last looked but perhaps now". There is no reliable way to determine that a thread is still</em> inside its run method, even if there was a reliable way to determine the opposite. </p> <p> You can just join the thread if not already detached. </p> <p> </p> Ticket viboes Sun, 16 Feb 2014 09:45:36 GMT <link>https://svn.boost.org/trac10/ticket/9654#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9654#comment:5</guid> <description> <p> Duplicate <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9618" title="#9618: Bugs: try_join_for problem: program is not terminate. (closed: fixed)">#9618</a> </p> </description> <category>Ticket</category> </item> <item> <author>ernest.galbrun@…</author> <pubDate>Tue, 01 Jul 2014 14:29:36 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/9654 https://svn.boost.org/trac10/ticket/9654 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">0001-changed-the-copy-from-rvalue-reference-to-copy-m_taa.patch</span> </li> </ul> <p> patch proposing fix </p> Ticket