[英]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.