I\'m wondering if this construction will cause an error:
lock(sync)
{
// something
lock(sync)
{
//something
lock(sync)
{
//something
lock is a wrapper for Monitor.Enter and Monitor.Exit:
The
lockkeyword callsEnterat the start of the block andExitat the end of the block. From the former's documentation:
From the documentation for Monitor.Enter:
It is legal for the same thread to invoke
Entermore than once without it blocking; however, an equal number ofExitcalls must be invoked before other threads waiting on the object will unblock.
Because the calls to Enter and Exit are paired, your code pattern has well defined behaviour.
Note, however, that lock is not guaranteed to be an exception-less construct:
A
ThreadInterruptedExceptionis thrown ifInterruptinterrupts a thread that is waiting to enter alockstatement.