Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Mutex Class

July 28, 2014

A synchronization primitive that can also be used for interprocess synchronization.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public sealed class Mutex : WaitHandle

The Mutex type exposes the following members.

  NameDescription
Public methodMutex()Initializes a new instance of the Mutex class with default properties.
Public methodMutex(Boolean)Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex.
Public methodMutex(Boolean, String)Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex, and a string that is the name of the mutex.
Public methodMutex(Boolean, String, Boolean)Security Critical. Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex, a string that is the name of the mutex, and a Boolean value that, when the method returns, indicates whether the calling thread was granted initial ownership of the mutex.
Top

  NameDescription
Public propertySafeWaitHandleGets or sets the native operating-system handle. (Inherited from WaitHandle.)
Top

  NameDescription
Public methodCloseWhen overridden in a derived class, releases all resources held by the current WaitHandle. (Inherited from WaitHandle.)
Public methodDispose()Releases all resources used by the current instance of the WaitHandle class. (Inherited from WaitHandle.)
Protected methodDispose(Boolean)When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources. (Inherited from WaitHandle.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberOpenExistingSecurity Critical. Opens the specified named mutex, if it already exists.
Public methodReleaseMutexReleases the Mutex once.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodStatic memberTryOpenExistingSecurity Critical. Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.
Public methodWaitOne()Blocks the current thread until the current WaitHandle receives a signal. (Inherited from WaitHandle.)
Public methodWaitOne(Int32)Blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to specify the time interval. (Inherited from WaitHandle.)
Public methodWaitOne(TimeSpan)Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval. (Inherited from WaitHandle.)
Top

When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex.

The Mutex class enforces thread identity, so a mutex can be released only by the thread that acquired it.

If a thread terminates while owning a mutex, the mutex is said to be abandoned. The state of the mutex is set to signaled, and the next waiting thread gets ownership.

Caution noteCaution:

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.

In the case of a system-wide mutex, an abandoned mutex might indicate that an application has been terminated abruptly.

Mutexes are of two types: local mutexes, which are unnamed, and named system mutexes. A local mutex exists only within your process. It can be used by any thread in your process that has a reference to the Mutex object that represents the mutex. Each unnamed Mutex object represents a separate local mutex.

Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes. You can create a Mutex object that represents a named system mutex by using a constructor that accepts a name. The operating-system object can be created at the same time, or it can exist before the creation of the Mutex object. You can create multiple Mutex objects that represent the same named system mutex.

Version Notes

Windows Phone

 Mutex is supported in Windows Phone only.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

This type is thread safe.

Show:
© 2014 Microsoft