Saturday, 2 December 2017

Deadlock Sample in C#

public class DeadlockSamples
{
    private static object staticObjLockA = new Object();
    private static object staticObjLockB = new Object();

    public static void Main()
    {
        // Initialize thread with address of DoWork1
        Thread thread1 = new Thread(DoWork1);

        // Initilaize thread with address of DoWork2
        Thread thread2 = new Thread(DoWork2);

        // Start the Threads.
        thread1.Start();
        thread2.Start();

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

        // This statement will never be executed.
        Console.WriteLine("Done Processing...");
    }

    private static void DoWork1()
    {
        lock (staticObjLockA)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockB");

            // Sleep to yield.
            Thread.Sleep(1000);
            lock (staticObjLockB)
            {
                // This block will never be executed.
                Console.WriteLine("In DoWork1 Critical Section.");
                // Access some shared resource here.
            }
        }
    }

    private static void DoWork2()
    {
        lock (staticObjLockB)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockA");
            lock (staticObjLockA)
            {
                // This block will never be executed.
                Console.WriteLine("In DoWork2 Critical Section.");
                // Access some shared resource here.
            }
        }
    }
}

public class DeadlockSamplesMonitor
{
    private static object staticObjLockA = new Object();
    private static object staticObjLockB = new Object();

    public static void Main()
    {
        // Initialize thread with address of DoWork1
        Thread thread1 = new Thread(DoWork1);

        // Initilaize thread with address of DoWork2
        Thread thread2 = new Thread(DoWork2);

        // Start the Threads.
        thread1.Start();
        thread2.Start();

        thread1.Join();
        thread2.Join();
       
        Console.WriteLine("Done Processing...");
    }

    private static void DoWork1()
    {
        lock (staticObjLockA)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockB");

            // Sleep to yield.
            Thread.Sleep(1000);

            // This will try to acquire lock for 5 seconds.
            if (Monitor.TryEnter(staticObjLockB, 5000))
            {
                try
                {
                    // This block will never be executed.
                    Console.WriteLine("In DoWork1 Critical Section.");
                    // Access some shared resource here.
                }
                finally
                {
                    Monitor.Exit(staticObjLockB);
                }
            }
            else
            {
                // Print lock not able to acquire message.
                Console.WriteLine("Unable to acquire lock, exiting DoWork1.");
            }               
        }
    }

    private static void DoWork2()
    {
        lock (staticObjLockB)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockA");
            lock (staticObjLockA)
            {                   
                Console.WriteLine("In DoWork2 Critical Section.");
                // Access some shared resource here.
            }
        }
    }
}

No comments:

Post a Comment