Boost C++ Libraries: Ticket #12386: lock_error exception when using try_lock_for() on Windows https://svn.boost.org/trac10/ticket/12386 <p> The following program immediately crashes with a lock_error exception on Windows: </p> <pre class="wiki">#include &lt;boost/thread/locks.hpp&gt; #include &lt;boost/thread/mutex.hpp&gt; #include &lt;boost/thread/thread.hpp&gt; using namespace boost; shared_mutex mtx; void f() { while (true) { unique_lock&lt;shared_mutex&gt; lock(mtx, defer_lock); while (!lock.try_lock_for(chrono::milliseconds(1))) {} } } void g() { while (true) { shared_lock&lt;shared_mutex&gt; lock(mtx, defer_lock); while (!lock.try_lock_for(chrono::milliseconds(1))) {} } } int main() { const int N = 12; thread* threads[N]; for (int i = 0; i &lt; N; ++i) threads[i] = new thread(i % 2 ? f : g); for (int i = 0; i &lt; N; ++i) threads[i]-&gt;join(); return 0; } </pre><p> Callstack when the lock_error exception is thrown: </p> <pre class="wiki">test.exe!boost::throw_exception&lt;boost::lock_error&gt;(const boost::lock_error &amp; e) test.exe!boost::shared_mutex::try_lock_until(const boost::chrono::time_point&lt;boost::chrono::system_clock,boost::chrono::duration&lt;__int64,boost::ratio&lt;1,10000000&gt; &gt; &gt; &amp; tp) test.exe!boost::shared_mutex::try_lock_until&lt;boost::chrono::steady_clock,boost::chrono::duration&lt;__int64,boost::ratio&lt;1,1000000000&gt; &gt; &gt;(const boost::chrono::time_point&lt;boost::chrono::steady_clock,boost::chrono::duration&lt;__int64,boost::ratio&lt;1,1000000000&gt; &gt; &gt; &amp; t) test.exe!boost::shared_mutex::try_lock_for&lt;__int64,boost::ratio&lt;1,1000&gt; &gt;(const boost::chrono::duration&lt;__int64,boost::ratio&lt;1,1000&gt; &gt; &amp; rel_time) test.exe!boost::unique_lock&lt;boost::shared_mutex&gt;::try_lock_for&lt;__int64,boost::ratio&lt;1,1000&gt; &gt;(const boost::chrono::duration&lt;__int64,boost::ratio&lt;1,1000&gt; &gt; &amp; rel_time) </pre><p> Boost.Thread code that throws the exception, in boost\thread\win32\shared_mutex.hpp: </p> <pre class="wiki">bool try_lock_until(const chrono::time_point&lt;chrono::system_clock, chrono::system_clock::duration&gt;&amp; tp) { for(;;) { state_data old_state=state; for(;;) { state_data new_state=old_state; if(new_state.shared_count || new_state.exclusive) { ++new_state.exclusive_waiting; if(!new_state.exclusive_waiting) { boost::throw_exception(boost::lock_error()); // &lt;-- HERE } new_state.exclusive_waiting_blocked=true; } else { new_state.exclusive=true; } ... </pre><p> Tested with Boost 1.55 and Boost 1.61, both compiled with Visual Studio 2013 in x64 mode, on Windows 10 64-bit. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12386 Trac 1.4.3 viboes Mon, 15 Aug 2016 17:31:20 GMT owner, status changed https://svn.boost.org/trac10/ticket/12386#comment:1 https://svn.boost.org/trac10/ticket/12386#comment:1 <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> <p> Hi, </p> <p> I'm really sorry. I have never understood the original implementation. </p> <p> Any help would be much appreciated. </p> Ticket viboes Mon, 15 Aug 2016 17:32:16 GMT <link>https://svn.boost.org/trac10/ticket/12386#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12386#comment:2</guid> <description> <p> Can you try to comment </p> <pre class="wiki"> boost::throw_exception(boost::lock_error()); // &lt;-- HERE </pre><p> and let me know what is the behavior? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 15 Aug 2016 17:34:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12386#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12386#comment:3</guid> <description> <p> You can define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN to get the generic implementation. It is slower but IMHO it has less bugs. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Mon, 15 Aug 2016 17:59:09 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12386#comment:4 https://svn.boost.org/trac10/ticket/12386#comment:4 <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">duplicate</span> </li> </ul> <p> It seems that this is a duplicate of <a class="assigned ticket" href="https://svn.boost.org/trac10/ticket/11499" title="#11499: Bugs: windows - exception lock_error while intensive locking/unlocking of ... (assigned)">#11499</a>. </p> Ticket