Search code examples
c#.netmultithreadingc#-4.0deadlock

Creating a deadlock


I have some code that looks like the below. Does this create a deadlock?

private readonly object objectLock = new object();

public void MethodA()
{
    lock(objectLock)
    {
       MethodB();
    }
}

public void MethodB()
{
    lock(objectLock)
    {
      //do something
    }
}

UPDATE: There will 2 threads running


Solution

  • No its not a deadlock. Its the same thread locking on the same synchronization object. A thread can take nested locks. It just needs to release it equal no. of times.