c# - 如果父应用程序未释放互斥锁对象会发生什么

标签 c# multithreading mutex

我的应用程序中有一个命名的互斥量。

 private Mutex TaskExitStatus = new Mutex(false, "TaskExit")

我在使用 TaskExitStatus.WaitOne() 的方法中获取互斥锁

如果我在退出应用程序时不释放互斥量会发生什么?互斥锁会被安全处理吗?

更新 1: 如果未在应用程序退出时释放,命名和未命名互斥锁的行为是否相同?

更新 2 这是我从MSDN link读到的

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.

最佳答案

根据documentation , 它被遗弃(不一定被丢弃)

If a thread terminates while owning a mutex, the mutex is said to be abandoned

此外,请注意有关废弃互斥体的警告:

An abandoned mutex often indicates a serious error in the code. When a thread exits without releasing the mutex, the data structures protected by the mutex might not be in a consistent state. The next thread to request ownership of the mutex can handle this exception and proceed, if the integrity of the data structures can be verified.

关于c# - 如果父应用程序未释放互斥锁对象会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13028146/

相关文章:

c# - 如何在同一个 .aspx 页面上使用两个更新面板

c++ - 如何安全地关闭其中有无限循环的线程

java - Java 中的管道输出流

c++ - 为什么这段代码中调用了两次析构函数?

c++ - 互斥保护宏

c# - 查看/收集 OnPrem Service Fabric ETW 事件

c# - 将对象传递给用户控件和 + 未将对象引用设置为对象的实例

c++ - C++中唯一锁、互斥量和条件变量的关系

Python多处理安全地写入文件

c# - 从另一个类 C# .NET 调用表单类中的函数