Usage of Mutex in C# Programming

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

  • Scenario: In situations where multiple threads or processes require exclusive access to a shared resource, such as a file, database, or hardware device.
  • Example: Consider a multi-threaded application where several threads need to concurrently write to a shared log file.
  • Benefit: Utilizing a Mutex, it guarantees that only one thread or process can access the resource at a time, preventing data corruption or race conditions.

2. Cross-Process Synchronization

  • Scenario: When synchronization is necessary between threads running in different processes.
  • Example: Coordinating access to a shared memory-mapped file among multiple processes.
  • Benefit: A Mutex can be named and system-wide, enabling synchronization across process boundaries.

3. Critical Section Protection

  • Scenario: In cases where critical sections of code need protection from concurrent execution by multiple threads.
  • Example: Consider a cache implementation where adding or removing items must be thread-safe.
  • Benefit: By utilizing a Mutex, it facilitates the serialization of access to critical sections, ensuring that only one thread executes the protected code at a time, thus avoiding race conditions.

4. Resource Pooling

  • Scenario: When managing access to a limited pool of resources, such as database connections or network sockets.
  • Example: A connection pool where multiple threads compete for available connections.
  • Benefit: A Mutex can be employed to control access to the pool, guaranteeing that the number of simultaneous users does not exceed the pool's capacity.

5. Deadlock Avoidance

  • Scenario: In situations where multiple synchronization primitives are used together to prevent deadlock occurrences.
  • Example: Implementing a transactional system where multiple resources need to be locked atomically.
  • Benefit: A Mutex can participate in deadlock avoidance strategies, contributing to the prevention of deadlock situations.

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();
        }
    }
}