为什么锁确保底层监控被释放,显示器的直接使用不? [英] Why does the lock insure that the underlying monitor is released and direct usage of monitor does not?

查看:110
本文介绍了为什么锁确保底层监控被释放,显示器的直接使用不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN文章线程同步(C#编程指南)规定:

lock (x)
{
    DoSomething();
}



等同于:

is equivalent to:

System.Object obj = (System.Object)x;
System.Threading.Monitor.Enter(obj);
try
{
    DoSomething();
}
finally
{
    System.Threading.Monitor.Exit(obj);
} 



然后是:

and then that:

使用锁定关键字通常优于直接使用监视器
级,... 由于锁保证了基本的显示器
被释放,甚至如果受保护的代码抛出一个异常

"Using the lock keyword is generally preferred over using the Monitor class directly, ... because lock insures that the underlying monitor is released, even if the protected code throws an exception"

请问这句话的意思是,最后的代码片段,使用显示器时,不不保证的基本显示器被释放,即使受保护的代码抛出一个异常?结果
为什么呢?

Does this phrase mean that the last code snippet, using the monitor, does not insure that "the underlying monitor is released, even if the protected code throws an exception" ?
And why?

好吧,我大约相同的相互矛盾的断言等价物,而不是(一个使用保证,另外,相当,没有)混淆。

Well, I am confused by the contradicting each other assertions "equivalent" and not (one usage insures and another, equivalent, does not) about the same.

推荐答案

如果你看看由4.0编译器值为anycpu生成并扭转到C#的clossest锁IL equivelent实现我能得到的将是这样的:

If you have a look at the IL that is generated by the 4.0 compiler for anycpu and reverse that to C# the clossest lock equivelent implementation I could get at would look like:

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);
   }
}



所有做是为了防止这种情况,其中一个锁采取一个线程中止,锁永远不会释放,导致种族/僵局。警告告诉你基本平衡输入和良好的和formost失败的情况下退出呼叫。 。lock语句是实现这一目标的最简单的抽象

All is done to prevent the situation where a lock is taken, a thread aborts and the lock is never released, causing a race/deadlock. The warning tells you basically to balance Enter and Exit calls in good and formost failure situations. The lock statement is the simplest abstraction that achieves that goal.

在此基础上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

这篇关于为什么锁确保底层监控被释放,显示器的直接使用不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆