According to the language specification it lock(obj) statement;will be compiled as:
object lockObj = obj;
Monitor.Enter(lockObj);
try
{
statement;
}
finally
{
Monitor.Exit(lockObj);
}
However, it is compiled as:
try
{
object lockObj = obj;
bool lockTaken = false;
Monitor.Enter(lockObj, ref lockTaken);
statement;
}
finally
{
if (lockTaken) Monitor.Exit(lockObj);
}
This seems a lot more complicated than necessary. So the question is, what is the advantage of this implementation?
source
share