A sure-fire deadlock : Thread DeadLock « Thread « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading;

public class MainClass
{
    private static object deadlockA = new object();
    private static object deadlockB = new object();
    
    public static void Main()
    {
        lock (deadlockA) {
            ThreadPool.QueueUserWorkItem(delegate {
                lock (deadlockB) {
                    Console.WriteLine("A");
                    lock (deadlockA) {
                        Console.WriteLine("B");
                    }
                }
            });
            lock (deadlockB) {
                Console.WriteLine("Main: got b");
            }
        }

    }
}
Main: got b
A
B








20.13.Thread DeadLock
20.13.1.Dining Philosopher
20.13.2.A sure-fire deadlock