Mutex 对象可用于提供对资源的独占访问权限。You can use a Mutex object to provide exclusive access to a resource. 虽然 Mutex 类使用的系统资源比 Monitor 类更多,但它可以跨应用域边界进行封送,可用于多个等待操作以及同步不同进程中的线程。The Mutex class uses more system resources than the Monitor class, but it can be marshaled across application domain boundaries, it can be used with multiple waits, and it can be used to synchronize threads in different processes. 有关托管同步机制的比较,请参阅同步基元概述For a comparison of managed synchronization mechanisms, see Overview of Synchronization Primitives.

有关代码示例,请参阅 Mutex 构造函数的参考文档。For code examples, see the reference documentation for the Mutex constructors.

使用 MutexUsing Mutexes

线程调用 mutex 的 WaitOne 方法来请求获取所有权。A thread calls the WaitOne method of a mutex to request ownership. 在 mutex 可用或可选超时间隔结束前,该调用会一直处于阻止状态。The call blocks until the mutex is available, or until the optional timeout interval elapses. 如果没有任何线程拥有它,则 mutex 将处于已发出信号状态。The state of a mutex is signaled if no thread owns it.

线程通过调用 ReleaseMutex 方法来释放 mutex。A thread releases a mutex by calling its ReleaseMutex method. Mutex 具有线程关联;即 mutex 只能由拥有它的线程释放。Mutexes have thread affinity; that is, the mutex can be released only by the thread that owns it. 如果线程释放的 mutex 不是它拥有的,就会在线程中抛出 ApplicationExceptionIf a thread releases a mutex it does not own, an ApplicationException is thrown in the thread.

由于 Mutex 类派生自 WaitHandle,因此还可以调用 WaitHandle 的静态 WaitAllWaitAny 方法,以请求获取 Mutex 的所有权以及其他等待句柄。Because the Mutex class derives from WaitHandle, you can also call the static WaitAll or WaitAny methods of WaitHandle to request ownership of a Mutex in combination with other wait handles.

如果线程拥有 Mutex,此线程可以在重复的等待-请求调用中指定相同的 Mutex,而不会阻止执行;不过,它必须释放 Mutex,次数与释放所有权一样多。If a thread owns a Mutex, that thread can specify the same Mutex in repeated wait-request calls without blocking its execution; however, it must release the Mutex as many times to release ownership.

放弃的 mutexAbandoned Mutexes

如果线程终止而未释放 Mutex,则认为已放弃 mutex。If a thread terminates without releasing a Mutex, the mutex is said to be abandoned. 这通常指示存在严重的编程错误,因为该 mutex 正在保护的资源可能会处于不一致状态。This often indicates a serious programming error because the resource the mutex is protecting might be left in an inconsistent state. 在 .NET Framework 版本 2.0 中,AbandonedMutexException 在下一个获取 mutex 的线程中抛出。In the .NET Framework version 2.0, an AbandonedMutexException is thrown in the next thread that acquires the mutex.


在 .NET Framework 版本 1.0 和 1.1 中,放弃的 Mutex 设置为处于已收到信号状态,且下一个等待线程获得所有权。In the .NET Framework versions 1.0 and 1.1, an abandoned Mutex is set to the signaled state and the next waiting thread gets ownership. 如果没有等待线程,Mutex 一直处于已收到信号状态。If no thread is waiting, the Mutex remains in a signaled state. 不引发异常。No exception is thrown.

对于系统范围的 mutex,放弃的 mutex 可能指示应用程序已突然终止(例如,通过使用 Windows 任务管理器终止)。In the case of a system-wide mutex, an abandoned mutex might indicate that an application has been terminated abruptly (for example, by using Windows Task Manager).

本地 mutex 和系统 mutexLocal and System Mutexes

Mutex 有两种类型:本地 mutex 和命名系统 mutex。Mutexes are of two types: local mutexes and named system mutexes. 如果使用接受名称的构造函数创建 Mutex 对象,此对象与同名的操作系统对象相关联。If you create a Mutex object using a constructor that accepts a name, it is associated with an operating-system object of that name. 命名系统 mutex 在整个操作系统中都可见,并且可用于同步进程活动。Named system mutexes are visible throughout the operating system and can be used to synchronize the activities of processes. 可以创建多个表示同一命名系统 mutex 的 Mutex 对象,还能使用 OpenExisting 方法打开现有的命名系统 mutex。You can create multiple Mutex objects that represent the same named system mutex, and you can use the OpenExisting method to open an existing named system mutex.

本地 mutex 仅存在于进程中。A local mutex exists only within your process. 进程中引用本地 Mutex 对象的所有线程都可以使用本地 mutex。It can be used by any thread in your process that has a reference to the local Mutex object. 每个 Mutex 对象都是单独的本地 mutex。Each Mutex object is a separate local mutex.

系统 mutex 的访问控制安全性Access Control Security for System Mutexes

.NET Framework 2.0 版提供查询和设置命名系统对象的 Windows 访问控制安全性的能力。The .NET Framework version 2.0 provides the ability to query and set Windows access control security for named system objects. 建议从创建起立刻开始保护系统 mutex,因为系统对象是全局对象,因此可能被不是你自己的代码锁定。Protecting system mutexes from the moment of creation is recommended because system objects are global and therefore can be locked by code other than your own.

若要了解 mutex 访问控制安全性,请参阅 MutexSecurityMutexAccessRule 类、MutexRights 枚举、Mutex 类的 GetAccessControlSetAccessControlOpenExisting 方法,以及 Mutex(Boolean, String, Boolean, MutexSecurity) 构造函数。For information on access control security for mutexes, see the MutexSecurity and MutexAccessRule classes, the MutexRights enumeration, the GetAccessControl, SetAccessControl, and OpenExisting methods of the Mutex class, and the Mutex(Boolean, String, Boolean, MutexSecurity) constructor.

请参阅See also