如果您查看 4.0 编译器为 anycpu 生成的 IL 并将其反转为 C#,我可以得到的最接近的锁等效实现如下所示:
object x = new object();
bool lockTaken = false;
// lock
try{
System.Threading.Monitor.Enter(x, ref lockTaken)
DoSomeThing();
}
finally
{
if (lockTaken)
{
System.Threading.Monitor.Exit(x);
}
}
所做的一切都是为了防止出现锁被占用、线程中止并且锁永远不会释放的情况,从而导致竞争/死锁。警告基本上告诉您在良好和最失败的情况下平衡 Enter 和 Exit 调用。lock 语句是实现该目标的最简单的抽象。
基于此 IL:
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
.try
{
IL_0003: ldsfld object p::x
IL_0008: dup
IL_0009: stloc.1
IL_000a: ldloca.s 0
IL_000c: call void [mscorlib]System.Threading.Monitor::Enter(object, bool&)
IL_0011: nop
IL_0012: nop
IL_0013: call void p::DoSomething()
IL_0018: nop
IL_0019: nop
IL_001a: leave.s IL_002c
} // end .try
finally
{
IL_001c: ldloc.0
IL_001d: ldc.i4.0
IL_001e: ceq
IL_0020: stloc.2
IL_0021: ldloc.2
IL_0022: brtrue.s IL_002b
IL_0024: ldloc.1
IL_0025: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_002a: nop
IL_002b: endfinally
} // end handler
IL_002c: nop
IL_002d: ldsfld object p::x
IL_0032: call void [mscorlib]System.Threading.Monitor::Enter(object)
IL_0037: nop
.try
{
IL_0038: nop
IL_0039: call void p::DoSomething()
IL_003e: nop
IL_003f: nop
IL_0040: leave.s IL_0050
} // end .try
finally
{
IL_0042: nop
IL_0043: ldsfld object p::x
IL_0048: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_004d: nop
IL_004e: nop
IL_004f: endfinally
} // end handler
IL_0050: nop
IL_0051: ret