Mutex in C#

Mutex is a synchronization primitive utilized in C# to regulate the access to a shared resource among multiple threads or processes. Its purpose is to ensure that only one thread or process can acquire the mutex at any given time, thereby providing mutual exclusion. Below are various scenarios where a

Benefits of Mutex in C#

Mutex can be employed, along with the benefits it offers.

1. Exclusive Access to Shared Resource

2. Cross-Process Synchronization

3. Critical Section Protection

4. Resource Pooling

5. Deadlock Avoidance

Implementation of Mutex

Step 1. Xaml View for testing all cases.

<Window x:Class="MutexExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MutexExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel Orientation="Vertical">
            <Button x:Name="BtnSR" Height="30" Content="Shared Resource" Margin="10" Click="BtnSR_Click"/>
            <Button x:Name="BtnCPS" Height="30" Content="Cross-Process Synchronization" Margin="10" Click="BtnCPS_Click"/>
            <Button x:Name="BtnCSP" Height="30" Content="Critical Section Protection" Margin="10" Click="BtnCSP_Click"/>
            <Button x:Name="BtnRP" Height="30" Content="Resource Pooling" Margin="10" Click="BtnRP_Click"/>
            <Button x:Name="BtnDLA" Height="30" Content="Deadlock Avoidance" Margin="10" Click="BtnDLA_Click"/>
        </StackPanel>
    </Grid>
</Window>

WPF Application

Backend programming

using System;
using System.Threading;
using System.Windows;

namespace MutexExample
{
    public partial class MainWindow : Window
    {
        Mutex mutex = new Mutex();
        static int availableResources = 3;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void SharedResourceAccess(object id)
        {
            mutex.WaitOne(); // Acquire the mutex
            try
            {
                Console.WriteLine($"Thread {id} is accessing the shared resource...");
                // Simulate some work
                Thread.Sleep(2000);
            }
            finally
            {
                mutex.ReleaseMutex(); // Release the mutex
            }
        }

        private void BtnSR_Click(object sender, RoutedEventArgs e)
        {
            // Create multiple threads accessing the shared resource
            for (int i = 0; i < 5; i++)
            {
                Thread thread = new Thread(SharedResourceAccess);
                thread.Start(i);
            }
        }

        private void BtnCPS_Click(object sender, RoutedEventArgs e)
        {
            // Acquire the mutex
            if (mutex.WaitOne(TimeSpan.FromSeconds(5)))
            {
                try
                {
                    Console.WriteLine("Process 1 has acquired the mutex.");
                    Console.ReadLine(); // Hold the mutex until Enter is pressed
                }
                finally
                {
                    mutex.ReleaseMutex(); // Release the mutex
                }
            }
            else
            {
                Console.WriteLine("Process 1 failed to acquire the mutex.");
            }
        }

        private void CriticalSectionAccess(object id)
        {
            mutex.WaitOne(); // Acquire the mutex
            try
            {
                Console.WriteLine($"Thread {id} is executing the critical section...");
                // Simulate some work
                Thread.Sleep(2000);
            }
            finally
            {
                mutex.ReleaseMutex(); // Release the mutex
            }
        }

        private void BtnCSP_Click(object sender, RoutedEventArgs e)
        {
            // Create multiple threads accessing a critical section
            for (int i = 0; i < 5; i++)
            {
                Thread thread = new Thread(CriticalSectionAccess);
                thread.Start(i);
            }
        }

        private void ResourceAccess(object id)
        {
            mutex.WaitOne(); // Acquire the mutex
            try
            {
                if (availableResources > 0)
                {
                    availableResources--;
                    Console.WriteLine($"Thread {id} acquired a resource. Remaining resources: {availableResources}");
                    // Simulate some work
                    Thread.Sleep(2000);
                    availableResources++;
                }
                else
                {
                    Console.WriteLine($"Thread {id} could not acquire a resource. No resources available.");
                }
            }
            finally
            {
                mutex.ReleaseMutex(); // Release the mutex
            }
        }

        private void BtnRP_Click(object sender, RoutedEventArgs e)
        {
            // Create multiple threads to access the resource pool
            for (int i = 0; i < 5; i++)
            {
                Thread thread = new Thread(ResourceAccess);
                thread.Start(i);
            }
        }

        private void BtnDLA_Click(object sender, RoutedEventArgs e)
        {
            Thread thread1 = new Thread(Thread1);
            Thread thread2 = new Thread(Thread2);

            thread1.Start();
            thread2.Start();

            thread1.Join();
            thread2.Join();
        }

        private Mutex mutex1 = new Mutex();
        private Mutex mutex2 = new Mutex();

        private void Thread1()
        {
            mutex1.WaitOne();
            Console.WriteLine("Thread 1 acquired mutex1");

            Thread.Sleep(1000);

            mutex2.WaitOne();
            Console.WriteLine("Thread 1 acquired mutex2");

            mutex2.ReleaseMutex();
            mutex1.ReleaseMutex();
        }

        private void Thread2()
        {
            mutex2.WaitOne();
            Console.WriteLine("Thread 2 acquired mutex2");

            Thread.Sleep(1000);

            mutex1.WaitOne();
            Console.WriteLine("Thread 2 acquired mutex1");

            mutex1.ReleaseMutex();
            mutex2.ReleaseMutex();
        }
    }
}