Can someone please explain me why we need nested locking ?
look at this example :
lock (locker)
lock (locker)
lock (locker)
{
...
}

can someone please explain (+ example will be much appreciated).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is only an issue because you might have nested (eg mutually recursive) calls to methods that need to lock. Those methods must allow being called with the resource already locked but they can not depend on it. So nested locking is allowed, not needed.
The code you posted (and the book you refer to) show how it works by reducing it to an inline scenario. That is not ‘real’ code.
The simple rule is that a thread that already owns a lock can lock it again and that the number of Exits must match the number of Enters to release the lock.