CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    [RESOLVED] deadlock not occuring?

    I have the following code

    Code:
    private static readonly object Lock = new object();
    
    public static void Test(){ //call this Test1
      lock(Lock){
        Test(true);
      }
    }
    
    public static void Test(bool value){ //call this Test2
      lock(Lock){
        //do something
      }
    }
    Now, when I call the first Test() method without an parameter, shouldn't this result in a deadlock? Because Test1 is locks "Lock", then calls Test2, and now Test2 also wants to lock the same object. Am I missing something?

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: deadlock not occuring?

    try this...

    Code:
    public static void Test1(){ //call this Test1
      lock(Lock){
        Test2(true);
      }
    }
    
    public static void Test2(bool value){ //call this Test2
      lock(Lock){
        //do something
      }
    }

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: deadlock not occuring?

    Also works fine. (That was actually my original situation)

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: deadlock not occuring?

    A lock statement guarantees that only *one* thread can take that lock, but that one thread can take the lock as many times as they want. This scenario will not deadlock.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: deadlock not occuring?

    Along with what Mutant_Fruit has said. The same thread can acquire the lock multiple times, but different threads cannot. The whole point of thread synchronization is to prevent different threads from accessing the same resource. There is no problem when the same thread accesses the same resource multiple times.

    If you want a deadlock, modify the code to rename the methods Test1 and Test2 and add a Sleep statement to Test2. In one thread call Test2, and then call Test1 in another thread.

    Code:
     
    private static readonly object Lock = new object();
    
    public static void Test1(){ //call this from T1
      lock(Lock){
        Test2(true);
      }
    }
    
    public static void Test2(){ //call this from T2
      lock(Lock){
        Sleep( 10000 );
        //do something
      }
    }

  6. #6
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: deadlock not occuring?

    Thanks for your explanations. Now I understand it completly. But since the example is using only 1 object that is locked, a deadlock will not occur. It tried your example Arjay, but it executed just fine. Thread 1 and 2 are started at the same time, and 2 will wait with execution till 1 has finished.

    To get a deadlock, at least 2 different objects needs to be locked.
    Example that works
    Code:
          private static readonly object LockA = new object();
          private static readonly object LockB = new object();
    
          public static void Test1() {
             lock (LockA) {
                Thread.Sleep(1000);
                lock (LockB) {
                   Test2(true);
                }
             }
          }
    
          public static void Test2(object value) {
             lock (LockB) {
                Thread.Sleep(1000);
                lock (LockA) {
                   //do something
                }
             }
          }
    
          static void Main(string[] args) {
             Thread t = new Thread(new ParameterizedThreadStart(Test2));
             t.Start(false);
             Thread t2 = new Thread(Test1);
             t2.Start();
          }
    In this case, LockA is waiting for LockB and vice versa

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] deadlock not occuring?

    That's fine Danny. I generally write my code so it doesn't deadlock. Forgive me if I didn't get it right the first time.

  8. #8
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: [RESOLVED] deadlock not occuring?

    I ussualy also write my code so that it doesn't deadlock. But my trainee wrote some code that I thought would result in a deadlock, but it didn't. That explains my post :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured