简体   繁体   中英

std library equivalent to boost::upgrade_lock and boost::upgrade_to_unique_lock

I have just been giving the task of deleting boost dependencies with the std library. I have come across upgrade_lock and upgrade_to_unique_lock and was wondering if there is a equivalent std library class?

From cppreference on shared_mutex :

Shared mutexes do not support direct transition from shared to unique ownership mode: the shared lock has to be relinquished with unlock_shared() before exclusive ownership may be obtained with lock(). boost::upgrade_mutex may be used for this purpose.

So no.

The initial proposal N3568 by Howard Hinnant that proposed shared_mutex also contained a proposal for upgrade_mutex which fills exactly that gap. However the concurrency subgroup spoke against the introduction of upgrade_mutex into C++14. Consequently Howard wrote N3569 which only includes shared_mutex . That proposal was accepted into C++17.

Howard gives more detailed reasons for the removal in this answer and also states that he is not going to invest the time and energy needed to get upgrade_mutex standardized. As likely as sadly it will never be standardized.

Usually I would recommend using Boost for this. Since you explicitly want to replace Boost there are not too many options left.

One is using means provided by your target operating system which is non-portable and probably not what you want. This becomes easier if your implementation of <mutex> provides std::mutex::native_handle() .

You could also write your own solution, for that have a look at Howard's reference implementation of upgrade_mutex . Note that you cannot combine that with the std::lib directly since N3568 would have added seven constructors to std::unique_lock which user code obviously cannot do. Hence you would need to clone a suitably licensed implementation of <mutex> into your own namespace, combine it with the reference implementation and then add those constructors to unique_lock that you actually need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM