How to obtain an exclusive lock *first* and then downgrade to shared without releasing the lock

一曲冷凌霜 提交于 2019-12-05 08:57:18

Boost offers this functionality through the UpgradeLockable concept. The method you are looking for is unlock_and_lock_shared().

An implementation of this concept is provided by the upgrade_mutex class.

It seems the proper way to do this using lock adapters should be something like this:

boost::shared_mutex mtx;

void exclusive_to_shared( )
{
    boost::unique_lock< boost::shared_mutex > unique_lock( mtx );

    // The lock here is exclusive.

    boost::shared_lock< boost::shared_mutex > shared_lock( std::move( unique_lock ) );

    // The lock here is shared.
}

There's an explicit conversion defined from unique_lock's RV refs to shared_lock which calls the unlock_and_lock_shared( ). See this e-mail thread and the source.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!