Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the Mutex class in C#?
The Mutex class in C# is a synchronization primitive that provides mutually exclusive access to a shared resource. Unlike other synchronization mechanisms, Mutex can be used for both thread synchronization within a single process and interprocess synchronization across multiple processes.
The name "Mutex" stands for mutual exclusion, ensuring that only one thread can access a protected resource at a time.
Syntax
Following are the common ways to create a Mutex instance −
// Default constructor Mutex mutex = new Mutex(); // With initial ownership Mutex mutex = new Mutex(bool initiallyOwned); // With initial ownership and name Mutex mutex = new Mutex(bool initiallyOwned, string name);
Parameters
initiallyOwned − If
true, the calling thread initially owns the mutex; otherwise,false.name − The name of the mutex. If
null, the mutex is unnamed and local to the process.
Creating a Basic Mutex
Example
using System;
using System.Threading;
public class Demo {
private static Mutex mutex = new Mutex();
private static int counter = 0;
public static void Main() {
Thread t1 = new Thread(IncrementCounter);
Thread t2 = new Thread(IncrementCounter);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine("Final counter value: " + counter);
}
static void IncrementCounter() {
for (int i = 0; i
The output of the above code is −
Thread 3 incremented counter to 1
Thread 3 incremented counter to 2
Thread 3 incremented counter to 3
Thread 3 incremented counter to 4
Thread 3 incremented counter to 5
Thread 4 incremented counter to 6
Thread 4 incremented counter to 7
Thread 4 incremented counter to 8
Thread 4 incremented counter to 9
Thread 4 incremented counter to 10
Final counter value: 10
Using Named Mutex for Interprocess Synchronization
Example
using System;
using System.Threading;
public class Demo {
public static void Main() {
Mutex namedMutex = new Mutex(false, "GlobalMutexExample");
Console.WriteLine("Trying to acquire the named mutex...");
if (namedMutex.WaitOne(5000)) {
try {
Console.WriteLine("Mutex acquired successfully!");
Console.WriteLine("Performing critical work...");
Thread.Sleep(3000);
Console.WriteLine("Work completed.");
}
finally {
namedMutex.ReleaseMutex();
Console.WriteLine("Mutex released.");
}
}
else {
Console.WriteLine("Failed to acquire mutex within timeout.");
}
namedMutex.Dispose();
}
}
The output of the above code is −
Trying to acquire the named mutex...
Mutex acquired successfully!
Performing critical work...
Work completed.
Mutex released.
Mutex vs Other Synchronization Primitives
| Feature | Mutex | Monitor (lock) | Semaphore |
|---|---|---|---|
| Cross-process | Yes (with named mutex) | No | Yes (with named semaphore) |
| Thread affinity | Yes (only owner can release) | Yes | No |
| Performance | Slower (kernel object) | Faster (managed) | Slower (kernel object) |
| Resource count | One at a time | One at a time | Multiple (configurable) |
Common Use Cases
Single application instance − Ensure only one instance of your application runs.
Shared resource access − Control access to files, databases, or hardware devices across processes.
Critical section protection − Protect code sections that must not run concurrently.
Conclusion
The Mutex class in C# provides thread-safe and process-safe synchronization through mutual exclusion. It's particularly useful for interprocess synchronization using named mutexes, though it has higher overhead compared to lighter synchronization primitives like lock statements for single-process scenarios.
