A sure-fire deadlock : Thread DeadLock : Thread C# Examples


C# Examples » Thread » Thread DeadLock »

 

A sure-fire deadlock









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

        }
}
    
   
  
   



Output

Main: got b
A
B


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Thread
» Thread DeadLock