delphi - TMutex 在 Delphi 中是可重入的吗?
问题描述
我正在创建我的互斥锁:
FMutex := TMutex.Create(nil, False, 'SomeDumbText');
并在使用相同创建的互斥锁调用另一个方法的方法中使用它:
procedure a;
begin
FMutex.Acquire;
try
//do some work here and maybe call b
finally
FMutex.Release;
end;
end;
procedure b;
begin
FMutex.Acquire;
try
//do some work here
finally
FMutex.Release;
end;
end;
嵌套互斥体是否安全?
解决方案
TMutex
is implemented over the underlying platform object. On Windows that is the mutex object. On the other platforms that is the pthread mutex.
Your question is whether or not TMutex
is re-entrant. In turn, the answer depends on whether or not the underlying platform object is re-entrant. The Windows mutex is always re-entrant, the pthread mutex is optionally re-entrant, and the Delphi TMutex
code chooses to use it in re-entrant mode, by calling pthread_mutexattr_settype(Attr, PTHREAD_MUTEX_RECURSIVE)
.
So, the answer to your question is that TMutex
is re-entrant.
推荐阅读
- c# - 具有用户分配标识的 Azure 应用服务:在应用中检索 clientId?
- java - 多线程while循环
- spring - 关于“动态注入”的Spring Boot依赖注入的困惑
- python - Discord.py|用 id 替换频道
- javascript - 弹出模式关闭后如何从正文中删除css?
- node.js - 在 Sequelize 中为同一模型引用多个外键属性时出错
- json - 验证发送到 API 的表单数据?
- python - 用条件填充熊猫列
- r - 我需要什么样的功能来避免在 R studio 中重复动作?
- docker - Kubernetes “RETAIN” PV 没有在重启时自动挂载