A thread that owns a
recursive_mutex
object may acquire additional levels of ownership by callinglock()
ortry_lock()
on that object. It is unspecified how many levels of ownership may be acquired by a single thread. If a thread has already acquired the maximum level of ownership for arecursive_mutex
object, additional calls totry_lock()
shall fail, and additional calls tolock()
shall throw an exception of typesystem_error
.
“所有权的最高级别”是否有大于 1 的下限?递归 pthread 互斥锁怎么样?
最佳答案
标准中没有规定下限。这可能是故意的。
较旧的标准(我认为是 C)确实用于为此类事情提供下限。结果是人们编写了编码标准,规定您不能使用超过这些下限的值。例如:它曾经(我认为现在仍然是)实现定义了在比较相等时外部符号的多少个字符是重要的。所以 a_very_very_long_name_indeed_with_extra_padding
和 a_very_very_long_name_indeed_with_extra_paddingX
可能被视为相同的符号。最小长度被指定为 8 个字符,并且编写了编码标准来指定“外部符号的最大长度为八个字符”。
关于这个值的合理下限:我可以很容易地想象这个计数可能被打包到其他一些字段中,这样整个事情就可以用一个合适的指令自动更新。因此,少于 32 位可能会很划算。 (它只需要足够大以支持最大调用堆栈深度,因此在受限环境中,31 可能就足够了。)
关于c++ - recursive_mutex 的最大所有权级别的下限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37203741/