System.Threading.Mutex Class

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

See Also: Mutex Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Mutex : WaitHandle

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. System.Threading.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.

You can use the WaitHandle.WaitOne(int, bool) method to request ownership of a mutex. The thread that owns a mutex can request the same mutex in repeated calls to erload:System.Threading.WaitHandle.WaitOne without blocking its execution. However, the thread must call the Mutex.ReleaseMutex method the same number of times to release ownership of the mutex. The System.Threading.Mutex class enforces thread identity, so a mutex can be released only by the thread that acquired it. By contrast, the System.Threading.Semaphore class does not enforce thread identity.

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. Beginning in version 2.0 of the .NET Framework, an System.Threading.AbandonedMutexException is thrown in the next thread that acquires the abandoned mutex. Before version 2.0 of the .NET Framework, no exception was thrown.

Note:

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 (for example, by using Windows Task Manager).

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 System.Threading.Mutex object that represents the mutex. Each unnamed System.Threading.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 System.Threading.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 System.Threading.Mutex object. You can create multiple System.Threading.Mutex objects that represent the same named system mutex, and you can use the erload:System.Threading.Mutex.OpenExisting method to open an existing named system mutex.

Note:

On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0