[它山之石]看看C#中用lock关键实现同步的原理
object someObject = new object();
. . .
private void button1_Click(object sender, EventArgs e)
{
lock(someObject)
{
// Your code here
} }
利用ildasm打开编译过的代码,下面是生成的IL(中间语言)的代码:
.method private hidebysig instance void button1_Click(object sender, class [mscorlib]System.EventArgs e) cil managed
{
// Code size 29 (0x1d)
.maxstack 2
.locals init ([0] object CS$2$0000)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldfld object WindowsApplication1.Form1::someObject
IL_0007: dup
IL_0008: stloc.0
IL_0009: call void [mscorlib]System.Threading.Monitor::Enter(object)
IL_000e: nop
.try
{
IL_000f: nop
IL_0010: nop
IL_0011: leave.s IL_001b
} // end .try
finally
{
IL_0013: ldloc.0
IL_0014: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_0019: nop
IL_001a: endfinally
} // end handler
IL_001b: nop
IL_001c: ret
} // end of method Form1::button1_Click
然后再看看显示调用System.Threading.Monitor.Enter/Exit进行同步的代码:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Monitor.Enter(someObject);
try
{
// Your code here
}
finally
{
System.Threading.Monitor.Exit(someObject);
} }
察看它的IL:
.method private hidebysig instance void button1_Click(object sender, class [mscorlib]System.EventArgs e) cil managed
{
// Code size 34 (0x22)
.maxstack 1
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldfld object WindowsApplication1.Form1::someObject
IL_0007: call void [mscorlib]System.Threading.Monitor::Enter(object)
IL_000c: nop
.try
{
IL_000d: nop
IL_000e: nop
IL_000f: leave.s IL_0020
} // end .try
finally
{
IL_0011: nop
IL_0012: ldarg.0
IL_0013: ldfld object WindowsApplication1.Form1::someObject
IL_0018: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_001d: nop
IL_001e: nop
IL_001f: endfinally
} // end handler
IL_0020: nop
IL_0021: ret } // end of method Form1::button1_Click
通过比较很容易发现这两段代码生成的中间语言代码几乎是一样的,至此我们应该能够很明白lcok的工作原理了。