Skip to content

WINDOWS: Exception lock_error in shared_mutex.hpp #230

@caroline-clover

Description

@caroline-clover

Hi,
My testing program sometimes get exception lock_error.
According to dump, the exception is caused by the share_waiting overflow in bool timed_lock_shared(boost::system_time const& wait_until)
I've tried to modify the code in shared_mutex.hpp to avoid getting exception.
But I'm not sure if it's the right way to fix the issue or not.

Platform: Windows 10
Boost version: 1.62
Testing code:

boost::shared_mutex mtx;
int g_cnt = 5000000;
void f()
{
    while (g_cnt > 0)
    {
        boost::upgrade_lock<boost::shared_mutex> readlock(mtx);
        boost::upgrade_to_unique_lock<boost::shared_mutex> writelock(readlock);
        if (g_cnt > 0)
            --g_cnt;
    }
}
void g()
{
    while (g_cnt > 0)
    {
        boost::shared_lock<boost::shared_mutex> readlock(mtx);
    }
}
void h()
{
    while (g_cnt > 0)
    {
        boost::unique_lock<boost::shared_mutex> lock(mtx);
        if (g_cnt > 0)
            --g_cnt;
    }
}
int main()
{
    boost::thread t0(f);
    boost::thread t1(g);
    boost::thread t2(h);

    t0.join();
    t1.join();
    t2.join();
}

Related info in Dump:

//Stack
....
VCRUNTIME140!CxxThrowException+0xc2
boost::throw_exception<boost::lock_error>+0x3f [...\boost\boost\throw_exception.hpp @ 69] 
boost::shared_mutex::timed_lock_shared+0x297 [...\boost\boost\thread\win32\shared_mutex.hpp @ 169] 
boost::shared_mutex::lock_shared+0x17 (Inline Function @ 00007ff6`980f115c) [...\boost\boost\thread\win32\shared_mutex.hpp @ 144] 
boost::shared_lock<boost::shared_mutex>::lock+0x13c [...\boost\boost\thread\lock_types.hpp @ 645] 
boost::shared_lock<boost::shared_mutex>::{ctor}+0x14 (Inline Function @ 00007ff6`981384c4) [...\boost\boost\thread\lock_types.hpp @ 520] 
g+0x34
boost_thread_vc140_mt_1_62!boost::`anonymous namespace'::thread_start_function+0x43 [...\boost\boost_1_62_0\libs\thread\src\win32\thread.cpp @ 296] 
….

//value of old_state
@"old_state"                 [Type: boost::shared_mutex::state_data]
    [+0x000 (10: 0)] shared_count     : 0x0
    [+0x000 (21:11)] shared_waiting   : 0x7ff
    [+0x000 (22:22)] exclusive        : 0x1
    [+0x000 (23:23)] upgrade          : 0x0
    [+0x000 (30:24)] exclusive_waiting : 0x1
    [+0x000 (31:31)] exclusive_waiting_blocked : 0x1

Changes in shared_mutex.hpp:

         void release_shared_waiters(state_data old_state)
         {
-            if(old_state.shared_waiting || old_state.exclusive_waiting)
+            if(old_state.shared_waiting)
             {
-                BOOST_VERIFY(detail::win32::ReleaseSemaphore(semaphores[unlock_sem],old_state.shared_waiting + (old_state.exclusive_waiting?1:0),0)!=0);
+                BOOST_VERIFY(detail::win32::ReleaseSemaphore(semaphores[unlock_sem],old_state.shared_waiting,0)!=0);
             }

         }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions