Boost C++ Libraries: Ticket #12519: boost::thread::try_join_for does not return after timeout https://svn.boost.org/trac10/ticket/12519 <p> If we use BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC try_join_for does not abort after the given timeout anymore. If I remove the define it works again. </p> <p> We use the define to be able to use sleep_for while changing the system time. (See <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6787" title="#6787: Bugs: boost::thread::sleep() hangs if system time is rolled back (closed: fixed)">#6787</a>) </p> <p> To Reproduce: </p> <pre class="wiki"> #define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC #include &lt;iostream&gt; #include &lt;boost/thread.hpp&gt; void test_func() { boost::this_thread::sleep_for(boost::chrono::milliseconds(1500)); } int main() { boost::thread t(test_func); if (!t.try_join_for(boost::chrono::milliseconds(50))) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAILED" &lt;&lt; std::endl; } if (t.try_join_for(boost::chrono::milliseconds(2000))) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAILED" &lt;&lt; std::endl; } } </pre><p> If the define is available the first try_join_for waits till the thread is finished, the expected behavior would be that the first try_join_for would return before the thread is finished. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12519 Trac 1.4.3 viboes Fri, 04 Nov 2016 00:11:35 GMT <link>https://svn.boost.org/trac10/ticket/12519#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:1</guid> <description> <p> It seems that the reference ticket doesn't works yet. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 04 Nov 2016 00:12:02 GMT</pubDate> <title>owner changed https://svn.boost.org/trac10/ticket/12519#comment:2 https://svn.boost.org/trac10/ticket/12519#comment:2 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> </ul> Ticket viboes Fri, 30 Dec 2016 02:03:37 GMT <link>https://svn.boost.org/trac10/ticket/12519#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:3</guid> <description> <p> Just to be fixed, have you tried defining BOOST_THREAD_VERSION 4? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 30 Dec 2016 02:30:39 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/12519#comment:3" title="Comment 3">viboes</a>: </p> <blockquote class="citation"> <p> Just to be fixed, have you tried defining BOOST_THREAD_VERSION 4? </p> </blockquote> <p> Forget this comment, please. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 19 Feb 2017 10:52:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:5</guid> <description> <p> Please, could you check if </p> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/544eda51bdf0c62b2f29dea6fe1cf935ad62580e"><span class="icon">​</span>https://github.com/boostorg/thread/commit/544eda51bdf0c62b2f29dea6fe1cf935ad62580e</a> </p> <p> could solve the issue? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 19 Feb 2017 10:55:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:6</guid> <description> <p> Please, could you check if </p> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/544eda51bdf0c62b2f29dea6fe1cf935ad62580e"><span class="icon">​</span>https://github.com/boostorg/thread/commit/544eda51bdf0c62b2f29dea6fe1cf935ad62580e</a> </p> <p> could solve the issue? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 19 Feb 2017 10:58:58 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/12519#comment:7 https://svn.boost.org/trac10/ticket/12519#comment:7 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.64.0</span> </li> </ul> Ticket viboes Sun, 19 Feb 2017 11:01:25 GMT status changed https://svn.boost.org/trac10/ticket/12519#comment:8 https://svn.boost.org/trac10/ticket/12519#comment:8 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket lumosimann@… Wed, 22 Feb 2017 14:17:56 GMT <link>https://svn.boost.org/trac10/ticket/12519#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:9</guid> <description> <p> We now found a temporary fix for this problem. Please check </p> <p> <a class="ext-link" href="https://gist.github.com/lukasm91/9732228f33e92963bb8762dac519e79f"><span class="icon">​</span>https://gist.github.com/lukasm91/9732228f33e92963bb8762dac519e79f</a> </p> <p> Note that there are two bugs involved, one in sleep_for, and one in try_join_for. We found two functions where a differentiation between monotonic and non-monotonic time was missing, such that some functions always used monotonic, and others always used non-monotonic time. </p> <p> There is also an updated test file attached. Consider that when you undefine BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC, this currently needs to be done in libs/thread/src/pthread/thread.cpp too, otherwise the compiler won't find the correct template specialization. The old behavior can be reactivated by undefining USEFIXES. </p> <p> @viboes Could you please check this fix and incorperate it into boost? We know that most probably a few #ifdefs are missing, but this fix currently solves our problems. </p> </description> <category>Ticket</category> </item> <item> <author>lumosimann@…</author> <pubDate>Thu, 23 Feb 2017 08:16:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:10</guid> <description> <p> I have just added a set of google tests to the gist mentioned above (googletests.cpp). </p> <p> Note that I am not sure whether this is all the expected behavior. Especially, I am unsure about what the expected behavior is when I set the time backwards while a sleep_for is running and BOOST_THREAD_HAS... is undefined. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 24 Feb 2017 14:29:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:11</guid> <description> <p> I've rolled back the last change as it seems it introduce a regression. </p> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/9bbf9bed80836282263ec0bdea0adf5c1fa3621b"><span class="icon">​</span>https://github.com/boostorg/thread/commit/9bbf9bed80836282263ec0bdea0adf5c1fa3621b</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 24 Feb 2017 14:37:47 GMT</pubDate> <title>type changed https://svn.boost.org/trac10/ticket/12519#comment:12 https://svn.boost.org/trac10/ticket/12519#comment:12 <ul> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Patches</span> </li> </ul> <p> Thanks a lot for the analysis. Yes, I missed this, and mixed monotonic and non-monotonic clocks :( </p> <p> I'll try to merge your changes as soon as possible. </p> Ticket viboes Fri, 24 Feb 2017 15:19:23 GMT <link>https://svn.boost.org/trac10/ticket/12519#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:13</guid> <description> <p> I have some trouble with <a class="ext-link" href="https://gist.github.com/lukasm91/9732228f33e92963bb8762dac519e79f#file-boost_12510-patch-L53"><span class="icon">​</span>https://gist.github.com/lukasm91/9732228f33e92963bb8762dac519e79f#file-boost_12510-patch-L53</a> </p> <pre class="wiki">-#ifdef BOOST_THREAD_USES_CHRONO #include &lt;boost/chrono/system_clocks.hpp&gt; #include &lt;boost/chrono/ceil.hpp&gt; -#endif </pre><p> Why do you need to remove these lines? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 24 Feb 2017 15:34:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:14</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/12519#comment:13" title="Comment 13">viboes</a>: </p> <blockquote class="citation"> <p> I have some trouble with <a class="ext-link" href="https://gist.github.com/lukasm91/9732228f33e92963bb8762dac519e79f#file-boost_12510-patch-L53"><span class="icon">​</span>https://gist.github.com/lukasm91/9732228f33e92963bb8762dac519e79f#file-boost_12510-patch-L53</a> </p> <pre class="wiki">-#ifdef BOOST_THREAD_USES_CHRONO #include &lt;boost/chrono/system_clocks.hpp&gt; #include &lt;boost/chrono/ceil.hpp&gt; -#endif </pre></blockquote> <p> Oh, I see that it wouldn't compile if not Chrono is not there :( </p> <p> I would need to change this because unfortunately Boost.Thread can be used without Boost.Chrono. </p> <blockquote class="citation"> <p> Why do you need to remove these lines? </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 25 Feb 2017 21:50:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:15 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:15</guid> <description> <p> I've committed </p> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/c7348b29cf8bfa1272645d04784419d37e1e7db5"><span class="icon">​</span>https://github.com/boostorg/thread/commit/c7348b29cf8bfa1272645d04784419d37e1e7db5</a> </p> <p> Please, could you tell me if fixes the issue. </p> <p> You will need to compile Boost.Thread after uncommenting in the config.hpp file or compiling everything with -D </p> <pre class="wiki">//#define BOOST_THREAD_USEFIXES_TIMESPEC //#define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC </pre><p> As the commit says, this is a partial solution as it needs to define some compiler defines, but at least it could be used to check if the solution works. </p> <p> I order to don't need the flags I would need to be able to detect BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC and move inline, everything that could depend on it. </p> <p> I believe that I can remove BOOST_THREAD_USEFIXES_TIMESPEC, but is a little bit too late for boost 1.64. </p> </description> <category>Ticket</category> </item> <item> <author>lumosimann@…</author> <pubDate>Mon, 27 Feb 2017 07:18:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:16 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:16</guid> <description> <p> I now get a segfault due to infinite recursion. </p> <p> This is consequence of <a class="ext-link" href="https://github.com/boostorg/thread/commit/c7348b29cf8bfa1272645d04784419d37e1e7db5#diff-8a73888d7fa5a2b2b38f85db61376ba4L517"><span class="icon">​</span>https://github.com/boostorg/thread/commit/c7348b29cf8bfa1272645d04784419d37e1e7db5#diff-8a73888d7fa5a2b2b38f85db61376ba4L517</a> </p> <p> This line is for WIN32, I don't know whether this makes sense there, but for sure you need to add the fix in the linux section too, i.e. </p> <pre class="wiki">diff --git a/include/boost/thread/detail/thread.hpp b/include/boost/thread/detail/thread.hpp index 8dc6a9c..a67e8ea 100644 --- a/include/boost/thread/detail/thread.hpp +++ b/include/boost/thread/detail/thread.hpp @@ -542,7 +542,7 @@ namespace boost } #endif #ifdef BOOST_THREAD_USES_CHRONO - bool try_join_until(const chrono::time_point&lt;chrono::system_clock, chrono::nanoseconds&gt;&amp; tp) + bool try_join_until(const chrono::time_point&lt;my_clock_t, chrono::nanoseconds&gt;&amp; tp) { using namespace chrono; nanoseconds d = tp.time_since_epoch(); </pre><p> Otherwise we will infinitely recurse into try_join_until. </p> <p> Although this fix is only temporary, I would recommend renaming my_clock_t and move it to the detail-namespace. </p> <p> Could this also be a fix for <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/6787"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/6787</a>? </p> </description> <category>Ticket</category> </item> <item> <author>lumosimann@…</author> <pubDate>Fri, 21 Apr 2017 10:23:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:17 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:17</guid> <description> <p> Will this problem be fixed until Boost 1.65? </p> <p> We would still get a segfault when adding <code>BOOST_THREAD_USEFIXED_TIMESPEC</code> because the patch has not been applied correctly. </p> <p> I think the effort is very little because the solution is almost finished except that it needs to be rewritten in "boost-style" (e.g., probably we should not use a <code>typedef my_clock_t</code>). </p> </description> <category>Ticket</category> </item> <item> <author>anton.indrawan@…</author> <pubDate>Wed, 17 May 2017 11:52:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:18 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:18</guid> <description> <p> I did get the recursive call as well and the patch by lumosimann solved the problem. I also vote it to be integrated into the next release (Boost 1.65). </p> </description> <category>Ticket</category> </item> <item> <author>anton.indrawan@…</author> <pubDate>Wed, 17 May 2017 17:30:28 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:19 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:19</guid> <description> <p> @viboes: I see the following commit: </p> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/30dff7f84ac5731fabeb6727f3b947ac12c3508e"><span class="icon">​</span>https://github.com/boostorg/thread/commit/30dff7f84ac5731fabeb6727f3b947ac12c3508e</a> </p> <p> I am wondering why you replace timespec_now by timespec_now_realtime. My application hangs and it blocks indefinitely in boost::condition_variable::do_wait_for. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 04 Jun 2017 22:52:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:20 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:20</guid> <description> <p> Please, could you tell me if the problem is there yet? </p> <p> There were a lot of commits since then, in particular </p> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/bf4b38b0af892ac6e5e1f2daaf8f21b80da4d311"><span class="icon">​</span>https://github.com/boostorg/thread/commit/bf4b38b0af892ac6e5e1f2daaf8f21b80da4d311</a> </p> </description> <category>Ticket</category> </item> <item> <author>lumosimann@…</author> <pubDate>Tue, 06 Jun 2017 05:59:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:21 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:21</guid> <description> <p> Yes, the problem is still around, because the patch has not been applied correctly. Your commits are unrelated to this. </p> <p> The patch I submitted in post #<a class="ticket" href="https://svn.boost.org/trac10/ticket/12519#comment:9" title="Comment 9">comment:9</a> still works for me. You have applied this patch, but as I have already mentioned in comment #<a class="ticket" href="https://svn.boost.org/trac10/ticket/12519#comment:16" title="Comment 16">comment:16</a>, you applied the patch at the wrong place. I have provided another patch in that comment which works at least for me. </p> <p> I would recommend to </p> <ol><li>Fix what I posted in comment <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/16" title="#16: Feature Requests: way to defer thread creation needed (closed: None)">#16</a> </li><li>Remove <code>BOOST_THREAD_USEFIXES_TIMESPEC</code> and make it default </li><li>Rename <code>my_clock_t</code> into something more descriptive and move into <code>detail</code> namespace </li><li>Test whether and how this fix is also needed for windows (this I really don't know) </li></ol><p> As I mentionned in #<a class="ticket" href="https://svn.boost.org/trac10/ticket/12519#comment:9" title="Comment 9">comment:9</a>, there are also unittests in the github-gist given in that comment (I would post it again, but the bug tracking system does not allow me to post that link to github). </p> <p> When you run those tests, you will see that <code>BOOST_THREAD_USEFIXES_TIMESPEC</code> + <code>BOOST_THREAD_CONDATTR_SET_CLOCK_MONOTONIC</code> currently results in a segfault, and all other combinations result in at least one error. </p> <p> The fix in comment #<a class="ticket" href="https://svn.boost.org/trac10/ticket/12519#comment:16" title="Comment 16">comment:16</a> plus defining <code>BOOST_THREAD_USEFIXES_TIMESPEC</code> resolves all unittests with and without <code>BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC</code> set accordingly. </p> <p> Doing this would solve both issues <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/12519" title="#12519: Patches: boost::thread::try_join_for does not return after timeout (closed: fixed)">#12519</a> and <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6787" title="#6787: Bugs: boost::thread::sleep() hangs if system time is rolled back (closed: fixed)">#6787</a>. Therefore I would strongly vote to integrate this into the upcoming boost version. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 10 Jun 2017 09:08:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:22 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:22</guid> <description> <p> I will take a look again to the patches. Please, could you provide a github PR? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 10 Jun 2017 09:30:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:23 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:23</guid> <description> <p> I would </p> <ol><li>take in account what you posted in comment <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/16" title="#16: Feature Requests: way to defer thread creation needed (closed: None)">#16</a> (I forget it surely) </li><li>Define BOOST_THREAD_USEFIXES_TIMESPEC for the time being </li><li>Rename my_clock_t into detail::internal_clock_t </li></ol><p> I have no time now to check for windows :( </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 12 Jun 2017 19:23:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:24 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:24</guid> <description> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/d4cff01c724f1355fef0990f3d6d678e52f61fd0"><span class="icon">​</span>https://github.com/boostorg/thread/commit/d4cff01c724f1355fef0990f3d6d678e52f61fd0</a> </p> </description> <category>Ticket</category> </item> <item> <author>lumosimann@…</author> <pubDate>Tue, 13 Jun 2017 10:36:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12519#comment:25 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12519#comment:25</guid> <description> <p> I had a look at your commit. Seems fine to me and works with my tests. </p> <p> You could post this to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/6787" title="#6787: Bugs: boost::thread::sleep() hangs if system time is rolled back (closed: fixed)">#6787</a> as well, which is likely to be fixed by now. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 24 Jun 2017 14:35:49 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/12519#comment:26 https://svn.boost.org/trac10/ticket/12519#comment:26 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.64.0</span> → <span class="trac-field-new">Boost 1.65.0</span> </li> </ul> Ticket viboes Sat, 24 Jun 2017 14:56:45 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/12519#comment:27 https://svn.boost.org/trac10/ticket/12519#comment:27 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket