I understand this is a single threaded example and this would lock in a multi-threaded example. But given the execution sequence, shouldn't the lock be consumed by the parent, therefore starving the child even in a single thread situation?
class Program
{
static readonly object Lock = new object();
static void Main(string[] args)
{
Console.WriteLine("Main start");
Parent();
Console.WriteLine("Main end");
Console.ReadLine();
}
static void Parent()
{
lock (Lock)
{
Console.WriteLine("Parent start");
Child();
Console.WriteLine("Parent end");
}
}
static void Child()
{
lock (Lock)
{
Console.WriteLine("Child start");
Console.WriteLine("Child end");
}
}
}
Console output
Main start
Parent start
Child start
Child end
Parent end
Main end