codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

46 / 60 topics
44Concurrency in C#45Threading in C#46Locks and Mutexes in C#47Memory Management in C#48Reflection in C#49Attributes in C#
Tutorials/C# Programming/Locks and Mutexes in C#
🔷C# Programming

Locks and Mutexes in C#

Updated 2026-05-15
10 min read

Locks and Mutexes in C#

Introduction

In the world of concurrent programming, ensuring that shared resources are accessed safely by multiple threads is a critical concern. Locks and Mutexes are synchronization primitives provided by the .NET framework to help manage thread safety effectively.

  • Locks: These are used to ensure that only one thread can execute a block of code at a time.
  • Mutexes: These provide similar functionality but can be used across different processes, not just within a single application.

In this tutorial, we will explore how to use locks and mutexes in C# to manage thread safety effectively. We'll cover the basics of each, their differences, and practical examples to illustrate their usage.

Concept

Locks

A lock is a synchronization mechanism that ensures only one thread can execute a block of code at a time. This prevents race conditions where multiple threads access shared resources concurrently, leading to unpredictable behavior or data corruption.

In C#, you typically use the lock statement to acquire and release a lock on an object. Here's how it works:

  1. Acquire Lock: A thread acquires the lock by entering the lock block.
  2. Execute Code: The thread executes the code within the lock block.
  3. Release Lock: Once the thread exits the lock block, the lock is automatically released.

Mutexes

A mutex (mutual exclusion object) is a synchronization primitive that can be used to protect shared resources from being simultaneously accessed by multiple threads or processes. Unlike locks, which are scoped to a single application domain, mutexes can be used across different processes.

Mutexes provide two main methods:

  • WaitOne: Blocks the thread until the mutex becomes available.
  • ReleaseMutex: Releases the mutex, allowing other threads or processes to acquire it.

Examples

Using Locks

Let's start with a simple example using locks to ensure that only one thread can modify a shared resource at a time.

using System;
using System.Threading;

class Program
{
    private static readonly object lockObject = new object();
    private static int counter = 0;

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

    private static void IncrementCounter()
    {
        for (int i = 0; i < 1000; i++)
        {
            lock (lockObject)
            {
                counter++;
            }
        }
    }
}

In this example, two threads (`t1` and `t2`) increment a shared counter variable. The `lock` statement ensures that only one thread can enter the critical section at a time, preventing race conditions.

### Using Mutexes

Now, let's see how to use mutexes to achieve similar functionality but across different processes.

```csharp
using System;
using System.Threading;

class Program
{
    private static Mutex mutex = new Mutex(false, "Global\\MyMutex");

    static void Main()
    {
        try
        {
            Console.WriteLine("Attempting to acquire the mutex...");
            if (mutex.WaitOne(1000)) // Wait up to 1 second for the mutex
            {
                try
                {
                    Console.WriteLine("Mutex acquired. Performing critical section work...");
                    Thread.Sleep(2000); // Simulate some work
                }
                finally
                {
                    Console.WriteLine("Releasing the mutex...");
                    mutex.ReleaseMutex();
                }
            }
            else
            {
                Console.WriteLine("Failed to acquire the mutex within the timeout period.");
            }
        }
        catch (AbandonedMutexException)
        {
            Console.WriteLine("The mutex was abandoned by another process.");
        }
    }
}

In this example, a global mutex named "Global\\MyMutex" is created. The `WaitOne` method attempts to acquire the mutex, and if successful, it performs some critical section work before releasing the mutex with `ReleaseMutex`. If the mutex cannot be acquired within the specified timeout period, an error message is displayed.

## What's Next?

In the next section, we will delve into **Memory Management in C#**, exploring how to manage memory effectively in .NET applications. Understanding memory management is crucial for optimizing performance and avoiding common pitfalls such as memory leaks.

Stay tuned!

PreviousThreading in C#Next Memory Management in C#

Recommended Gear

Threading in C#Memory Management in C#