Boost C++ Libraries: Ticket #12949: using sleep_for in a thread context without including boost/thread/thread.hpp yields incorrect behaviour when BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC is defined https://svn.boost.org/trac10/ticket/12949 <p> When defining a function f() in a compilation unit which uses boost::thread::sleep_for() which does not include &lt;boost/thread/thread.hpp&gt;, but another boost thread header, </p> <p> function.h </p> <pre class="wiki">#ifndef FUNCTION_H #define FUNCTION_H void f(); #endif </pre><p> function.cpp: </p> <pre class="wiki">#define BOOST_THREAD_VERSION 4 #define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC #include &lt;boost/thread/condition_variable.hpp&gt; #include "function.h" void f() { boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); } </pre><p> and defining a compilation unit which uses this function in a thread context, </p> <p> main.cpp: </p> <pre class="wiki">#define BOOST_THREAD_VERSION 4 #define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC #include "function.h" #include &lt;boost/thread.hpp&gt; int main() { auto t = boost::thread([] { f(); }); t.join(); return 0; } </pre><p> the sleep_for() waits much long than expected when a monotonic clock is used. </p> <p> This has been tested with both boost versions 1.63.0 and 1.64.0 beta2. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12949 Trac 1.4.3 John Maddock Sun, 16 Apr 2017 08:52:35 GMT component changed; owner set https://svn.boost.org/trac10/ticket/12949#comment:1 https://svn.boost.org/trac10/ticket/12949#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Anthony Williams</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">thread</span> </li> </ul> Ticket viboes Sat, 29 Apr 2017 02:08:46 GMT <link>https://svn.boost.org/trac10/ticket/12949#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12949#comment:2</guid> <description> <p> This </p> <pre class="wiki">#define BOOST_THREAD_VERSION 4 #define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC #include &lt;boost/thread/condition_variable.hpp&gt; #include "function.h" void f() { boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); // ** } </pre><p> shouldn't compile. You need to include </p> <pre class="wiki">&lt;boost/thread/thread.hpp&gt; </pre><p> I will why it compiles. </p> <p> Are you testing on Windows? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 29 Apr 2017 02:08:52 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/12949#comment:3 https://svn.boost.org/trac10/ticket/12949#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket viboes Sat, 29 Apr 2017 02:30:28 GMT <link>https://svn.boost.org/trac10/ticket/12949#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12949#comment:4</guid> <description> <p> Could you be more specific about what incorrect behavior do you get. What is the difference in time? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 29 Apr 2017 15:59:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12949#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12949#comment:5</guid> <description> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/da83662e1aa3ef045de719f3ad23761c26369675"><span class="icon">​</span>https://github.com/boostorg/thread/commit/da83662e1aa3ef045de719f3ad23761c26369675</a> </p> </description> <category>Ticket</category> </item> <item> <author>rkaris@…</author> <pubDate>Mon, 01 May 2017 10:29:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12949#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12949#comment:6</guid> <description> <p> I'm testing on linux in this case. It seems the sleep_for() in the example selects the sleep_for() nanoseconds overload found in boost/thread/pthread/thread_data.hpp instead of the boost/thread/v2/thread.hpp one. With the monotonic clock defined, this seems to lead to a very long sleeping period. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 24 Aug 2017 05:54:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12949#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12949#comment:7</guid> <description> <p> Sorry for the late response. </p> <p> Please could you try adding </p> <pre class="wiki">#include &lt;boost/thread/v2/thread.hpp&gt; </pre><p> in &lt;boost/thread/pthread/condition_variable.hpp&gt; </p> <pre class="wiki">#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS #include &lt;boost/thread/pthread/thread_data.hpp&gt; #include &lt;boost/thread/v2/thread.hpp&gt; #endif </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 24 Aug 2017 06:22:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12949#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12949#comment:8</guid> <description> <p> I have another idea. What about provoking a linker error </p> <pre class="wiki">Undefined symbols for architecture x86_64: "void boost::this_thread::sleep_for&lt;long long, boost::ratio&lt;1l, 1000l&gt; &gt;(boost::chrono::duration&lt;long long, boost::ratio&lt;1l, 1000l&gt; &gt; const&amp;)", referenced from: </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Thu, 24 Aug 2017 06:23:09 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/12949#comment:9 https://svn.boost.org/trac10/ticket/12949#comment:9 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.66.0</span> </li> </ul> Ticket viboes Sat, 26 Aug 2017 09:45:35 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/12949#comment:10 https://svn.boost.org/trac10/ticket/12949#comment:10 <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> <p> <a class="ext-link" href="https://github.com/boostorg/thread/commit/a02f0ec577644ca45cdca20e91cd8cd82e6c2017"><span class="icon">​</span>https://github.com/boostorg/thread/commit/a02f0ec577644ca45cdca20e91cd8cd82e6c2017</a> </p> Ticket