I am trying to understand to source code of boost shared_mutex, but get stuck in the unlock_shared()
method.
Following code copy from boost1.68, line 241 ~ 264:
void unlock_shared()
{
boost::unique_lock<boost::mutex> lk(state_change);
state.assert_lock_shared();
state.unlock_shared();
if (state.no_shared())
{
if (state.upgrade)
{
// As there is a thread doing a unlock_upgrade_and_lock that is waiting for state.no_shared()
// avoid other threads to lock, lock_upgrade or lock_shared, so only this thread is notified.
state.upgrade=false;
state.exclusive=true;
//lk.unlock();
upgrade_cond.notify_one();
}
else
{
state.exclusive_waiting_blocked=false;
//lk.unlock();
}
release_waiters();
}
}
When last reader unlock_shared, if there is an upgrade lock is upgrading, it will set the state.upgrade
to false
and state.exclusive
to true
then notify upgrade_cond
.
I understand that set state.exclusive
to true
can avoid other threads to lock
, lock_upgrade
or lock_shared
.
But why set state.upgrade
to false
? If remove this line, what will happen?