Mutex Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

Inheritance Hierarchy

System..::.Object
System.Threading..::.WaitHandle
System.Threading..::.Mutex

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

Syntax

Public NotInheritable Class Mutex _
    Inherits WaitHandle
public sealed class Mutex : WaitHandle

The Mutex type exposes the following members.

Constructors

Name Description
Mutex()()() Initializes a new instance of the Mutex class with default properties.
Mutex(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.
Mutex(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.
Mutex(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

Properties

Name Description
SafeWaitHandle Gets or sets the native operating-system handle. (Inherited from WaitHandle.)

Top

Methods

Name Description
Close When overridden in a derived class, releases all resources held by the current WaitHandle. (Inherited from WaitHandle.)
Dispose()()() Releases all resources used by the current instance of the WaitHandle class. (Inherited from WaitHandle.)
Dispose(Boolean) When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources. (Inherited from WaitHandle.)
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
OpenExisting Security Critical. Opens the specified named mutex, if it already exists.
ReleaseMutex Releases the Mutex once.
ToString Returns a string that represents the current object. (Inherited from Object.)
TryOpenExisting Security Critical. Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.
WaitOne()()() Blocks the current thread until the current WaitHandle receives a signal. (Inherited from WaitHandle.)
WaitOne(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.)
WaitOne(TimeSpan) Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval. (Inherited from WaitHandle.)

Top

Remarks

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.

Warning

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.

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Platforms

Windows Phone

Thread Safety

This type is thread safe.

See Also

Reference

System.Threading Namespace