gpt4 book ai didi

c++ - 互斥量的可升级所有权如何影响其他线程?

转载 作者:太空狗 更新时间:2023-10-29 21:45:50 27 4
gpt4 key购买 nike

情况是:

一个线程获得了 boost::shared_mutex 的可升级所有权并正在调用 unlock_upgrade_and_lock(),这会阻塞,因为此时其他线程正在拥有同一个 shared_mutex 的共享所有权。

当其他线程尝试“锁定共享”shared_mutex 时,第一个线程的可升级所有权是否会阻止(阻止)其他线程,以便所有已经共享所有权的线程最终将解锁_共享并保证第一个线程的独占所有权?

或者只要有另一个线程共享互斥锁,第一个线程就有可能一直处于阻塞状态?

最佳答案

(假设 Boost 实现模糊地模拟了 Howard Hinnant 的 WG21 提案......)

从共享所有权转换为升级所有权会阻止任何新线程获取锁,因此最终所有共享所有者都会释放它,具有升级所有权的线程可以将其转换为独占所有权。这就是“升级锁”相对于共享锁和独占锁的要点,请参阅 N3427 中的解释。 :

Note that an alternative design of try-converting from shared to exclusive, instead of from shared to upgrade as shown, would be susceptible to update (writer) starvation. This is because as long as there are multiple searchers (shared locks), none of the searchers can ever successfully try-convert to updaters. It is only by successfully registering yourself as the single thread having upgrade ownership and then blocking on a conversion from upgrade to exclusive, do you enable the implementation to start blocking new searchers from obtaining a shared lock so that you can eventually obtain the exclusive lock as existing searchers are cleared out.

关于c++ - 互斥量的可升级所有权如何影响其他线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16716906/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com