0
0
CsharpConceptBeginner · 4 min read

What is Deadlock in C#: Explanation and Example

In C#, a deadlock happens when two or more threads wait forever for each other to release resources, causing the program to freeze. It occurs when each thread holds a resource the other needs and neither can proceed.
⚙️

How It Works

Imagine two people trying to pass through a narrow hallway from opposite ends. Each person waits for the other to move first, so both get stuck. This is similar to a deadlock in C# where two threads each hold a lock on a resource and wait for the other to release their lock.

In programming, threads use locks to protect shared resources. If Thread A locks Resource 1 and waits for Resource 2, while Thread B locks Resource 2 and waits for Resource 1, neither can continue. This waiting cycle causes the program to freeze, which is called a deadlock.

💻

Example

This example shows two threads causing a deadlock by locking two objects in opposite order.

csharp
using System;
using System.Threading;

class DeadlockExample
{
    private static readonly object lockA = new object();
    private static readonly object lockB = new object();

    static void Thread1()
    {
        lock (lockA)
        {
            Console.WriteLine("Thread 1: Locked lockA");
            Thread.Sleep(100); // Simulate work
            lock (lockB)
            {
                Console.WriteLine("Thread 1: Locked lockB");
            }
        }
    }

    static void Thread2()
    {
        lock (lockB)
        {
            Console.WriteLine("Thread 2: Locked lockB");
            Thread.Sleep(100); // Simulate work
            lock (lockA)
            {
                Console.WriteLine("Thread 2: Locked lockA");
            }
        }
    }

    static void Main()
    {
        Thread t1 = new Thread(Thread1);
        Thread t2 = new Thread(Thread2);

        t1.Start();
        t2.Start();

        t1.Join();
        t2.Join();

        Console.WriteLine("Finished");
    }
}
Output
Thread 1: Locked lockA Thread 2: Locked lockB
🎯

When to Use

Deadlocks are problems to avoid, not situations to use intentionally. Understanding deadlocks helps you write safer multi-threaded C# programs. You should be careful when multiple threads access shared resources and use locks.

Real-world cases include database transactions, file access, or any situation where multiple threads or processes need exclusive access to resources. Avoiding deadlocks improves program reliability and user experience.

Key Points

  • Deadlock happens when threads wait forever for each other’s locked resources.
  • It causes the program to freeze or hang.
  • Careful lock ordering and timeout strategies help prevent deadlocks.
  • Detecting deadlocks early improves program stability.

Key Takeaways

Deadlock occurs when threads wait endlessly for resources locked by each other.
It causes your C# program to freeze and stop responding.
Avoid deadlocks by locking resources in a consistent order.
Use timeout or try-lock patterns to detect and handle deadlocks.
Understanding deadlocks helps build reliable multi-threaded applications.