简体   繁体   English

std 库等价于 boost::upgrade_lock 和 boost::upgrade_to_unique_lock

[英]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.我刚刚完成了使用 std 库删除 boost 依赖项的任务。 I have come across upgrade_lock and upgrade_to_unique_lock and was wondering if there is a equivalent std library class?我遇到了 upgrade_lock 和 upgrade_to_unique_lock 并且想知道是否有等效的标准库 class?

From cppreference on shared_mutex :shared_mutex上的 cppreference :

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().共享互斥锁不支持从共享所有权模式直接转换到唯一所有权模式:必须先使用 unlock_shared() 放弃共享锁,然后才能使用 lock() 获得独占所有权。 boost::upgrade_mutex may be used for this purpose. boost::upgrade_mutex 可用于此目的。

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. Howard Hinnant 提出的shared_mutex的最初提案N3568还包含一个upgrade_mutex提案,它正好填补了这个空白。 However the concurrency subgroup spoke against the introduction of upgrade_mutex into C++14.然而,并发小组反对将upgrade_mutex引入 C++14。 Consequently Howard wrote N3569 which only includes shared_mutex .因此,霍华德编写了N3569 ,其中仅包含shared_mutex That proposal was accepted into C++17.该提案被 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.霍华德在此答案中给出了删除的更详细原因,并表示他不会投入时间和精力来使upgrade_mutex标准化。 As likely as sadly it will never be standardized.可悲的是,它可能永远不会被标准化。

Usually I would recommend using Boost for this.通常我会建议为此使用 Boost。 Since you explicitly want to replace Boost there are not too many options left.由于您明确想要替换 Boost,因此没有太多选择。

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() .如果您的<mutex>实现提供std::mutex::native_handle() ,这将变得更容易。

You could also write your own solution, for that have a look at Howard's reference implementation of upgrade_mutex .您也可以编写自己的解决方案,查看 Howard 的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.请注意,您不能将其与 std::lib 直接结合,因为 N3568 会向std::unique_lock添加七个构造函数,而用户代码显然无法做到这一点。 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.因此,您需要将<mutex>的适当许可实现克隆到您自己的命名空间中,将其与参考实现结合起来,然后将这些构造函数添加到您实际需要的unique_lock中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 STL中的boost :: upgrade_to_unique_lock等效于什么? - What is the equivalent of boost::upgrade_to_unique_lock in STL? Boost upgrade_to_unique_lock编译错误 - Boost upgrade_to_unique_lock compilation error boost :: interprocess有&#39;upgrade_to_unique_lock&#39;吗? - Is there an 'upgrade_to_unique_lock' for boost::interprocess? boost :: unique_lock和boost :: upgrade_lock之间的区别? - Difference between boost::unique_lock and boost::upgrade_lock? 如何解锁boost :: upgrade_to_unique_lock(由boost :: shared_mutex制作)? - How to unlock boost::upgrade_to_unique_lock (made from boost::shared_mutex)? 提升upgrade_lock和DCLP(仔细检查的锁定模式) - Boost upgrade_lock and DCLP (double-checked locking pattern) 如何使boost_to_unique_lock + condition_variable协同工作? - how to make boost upgrade_to_unique_lock + condition_variable work together? Linux上的Boost 1.48.0 upgrade_to_unique_lock:自1.47以来是否有所更改,或者我做错了? - Boost 1.48.0 upgrade_to_unique_lock on Linux: Has something changed since 1.47 or I do something wrong? 使用boost :: upgrade_lock和shared_lock实现读/写线程安全 - Using boost::upgrade_lock and shared_lock to implement read/write thread safety 如何使用“upgrade_to_unique_lock”执行“try_lock” - How to do a “try_lock” with an “upgrade_to_unique_lock”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM