I’m learning multi-threading in C# and I saw a code below
static readonly object _locker = new object();
static void Main()
{
lock (_locker)
{
AnotherMethod();
// ...some work is going on
}
}
static void AnotherMethod()
{
lock (_locker) { Console.WriteLine ("Another method"); }
}
I wonder when does it require to use nested locking? Why don’t use only one lock in this case?
My first response would be that AnotherMethod can be called directly, not going through the Main method, so therefor you might need nested locking.