Monitor.TryEnter(object) 和 Monitor.TryEnter(object, ref bool) 之间存在什么重要区别?
看来这些代码片段的行为应该相同:
1: Monitor.TryEnter(object)
if (Monitor.TryEnter(lockObject))
{
try
{
DoSomething();
}
finally
{
Monitor.Exit(lockObject);
}
}
2: Monitor.TryEnter(object, ref bool) - 在 .NET 4.0 中引入,
bool lockAcquired;
try
{
Monitor.TryEnter(lockObject, ref lockAcquired);
if (lockAcquired)
{
DoSomething();
}
}
finally
{
if (lockAcquired)
{
Monitor.Exit(lockObject);
}
}
我从 MSDN 文档中看到 采用 ref bool
参数的重载:
如果由于以下原因未获得锁定 抛出异常,变量 为 lockTaken 参数指定 此方法结束后为 false。这 允许程序确定,在 所有情况,是否有必要 释放锁。
但文档还指出,仅采用 object
参数的重载不会引发除 ArgumentNullException
之外的异常。因此,如果上面的代码片段 1 中抛出异常,那可能只是因为 lockObject
为 null
,在这种情况下没有锁无论如何,都会被获取(并且 TryEnter
会返回 false
),因此不需要 Monitor.Exit
调用。
显然,他们不会无缘无故地引入这种过载。那么 Monitor.TryEnter(object, ref bool)
方法旨在解决什么场景?
It seems that these code snippets ought to behave identically:
1: Monitor.TryEnter(object)
if (Monitor.TryEnter(lockObject))
{
try
{
DoSomething();
}
finally
{
Monitor.Exit(lockObject);
}
}
2: Monitor.TryEnter(object, ref bool) - introduced in .NET 4.0
bool lockAcquired;
try
{
Monitor.TryEnter(lockObject, ref lockAcquired);
if (lockAcquired)
{
DoSomething();
}
}
finally
{
if (lockAcquired)
{
Monitor.Exit(lockObject);
}
}
I see from the MSDN documentation on the overload taking a ref bool
parameter:
If the lock was not taken because an
exception was thrown, the variable
specified for the lockTaken parameter
is false after this method ends. This
allows the program to determine, in
all cases, whether it is necessary to
release the lock.
But the documentation also states that the overload taking only the object
parameter throws no exceptions other than ArgumentNullException
. So it seems like if an exception were thrown in code snippet 1 above, it could only be because lockObject
is null
, in which case no lock was taken (and TryEnter
would've returned false
) anyway, so the Monitor.Exit
call would not be needed.
Clearly they would not have introduced this overload for no reason. So what scenario is the Monitor.TryEnter(object, ref bool)
method intended to address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅:锁和异常不混合
See: Locks and exceptions do not mix