[Boost-bugs] [Boost C++ Libraries] #12386: lock_error exception when using try_lock_for() on Windows

Subject: [Boost-bugs] [Boost C++ Libraries] #12386: lock_error exception when using try_lock_for() on Windows
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-08-09 21:30:32


#12386: lock_error exception when using try_lock_for() on Windows
------------------------------------+-------------------------
 Reporter: Franz Beaune <franz@…> | Owner: anthonyw
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: thread
  Version: Boost 1.61.0 | Severity: Showstopper
 Keywords: lock_error |
------------------------------------+-------------------------
 The following program immediately crashes with a lock_error exception on
 Windows:

 {{{
 #include <boost/thread/locks.hpp>
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/thread.hpp>

 using namespace boost;

 shared_mutex mtx;

 void f()
 {
     while (true)
     {
         unique_lock<shared_mutex> lock(mtx, defer_lock);
         while (!lock.try_lock_for(chrono::milliseconds(1))) {}
     }
 }

 void g()
 {
     while (true)
     {
         shared_lock<shared_mutex> 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 < N; ++i)
         threads[i] = new thread(i % 2 ? f : g);

     for (int i = 0; i < N; ++i)
         threads[i]->join();

     return 0;
 }
 }}}

 Callstack when the lock_error exception is thrown:

 {{{
 test.exe!boost::throw_exception<boost::lock_error>(const boost::lock_error
 & e)
 test.exe!boost::shared_mutex::try_lock_until(const
 boost::chrono::time_point<boost::chrono::system_clock,boost::chrono::duration<__int64,boost::ratio<1,10000000>
> > & tp)
 test.exe!boost::shared_mutex::try_lock_until<boost::chrono::steady_clock,boost::chrono::duration<__int64,boost::ratio<1,1000000000>
> >(const
 boost::chrono::time_point<boost::chrono::steady_clock,boost::chrono::duration<__int64,boost::ratio<1,1000000000>
> > & t)
 test.exe!boost::shared_mutex::try_lock_for<__int64,boost::ratio<1,1000>
>(const boost::chrono::duration<__int64,boost::ratio<1,1000> > & rel_time)
 test.exe!boost::unique_lock<boost::shared_mutex>::try_lock_for<__int64,boost::ratio<1,1000>
>(const boost::chrono::duration<__int64,boost::ratio<1,1000> > & rel_time)
 }}}

 Boost.Thread code that throws the exception, in
 boost\thread\win32\shared_mutex.hpp:

 {{{
 bool try_lock_until(const chrono::time_point<chrono::system_clock,
 chrono::system_clock::duration>& 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()); // <-- HERE
         }

         new_state.exclusive_waiting_blocked=true;
       }
       else
       {
         new_state.exclusive=true;
       }
       ...
 }}}

 Tested with Boost 1.55 and Boost 1.61, both compiled with Visual Studio
 2013 in x64 mode, on Windows 10 64-bit.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12386>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC