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

标签 c++ boost-thread

情况是:

一个线程获得了 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/

相关文章:

c++ - 这个结构怎么会有 sizeof == 0?

c++ - 需要对条件变量寻求的互斥保护(原子)赋值吗?

c++ - C/C++,NTFS。多个重解析点

c++ - 尝试在 C++ 中创建实例时“未声明的标识符”

c++ - 调用析构函数时出错

c++ - 使用 websocketpp 库在多线程程序中为 endpoint.listen() 创建一个单独的 boost 线程

gcc - 将 boost 版本从 1.52 切换到 1.53 后与 GCC 链接失败

c++ - 访问构建 boost::thread 的可调用对象

c++ - 用boost bind封装线程函数

c++ - 从递归返回结果的最有效方法是什么?