【发布时间】:2010-11-24 10:57:26
【问题描述】:
骗子: return statement in a lock procedure: inside or outside
标题有点误导。我知道你可以做到,但我想知道性能影响。
考虑这两个代码块。 (无错误处理)
这个块在锁外有return
public DownloadFile Dequeue()
{
DownloadFile toReturn = null;
lock (QueueModifierLockObject)
{
toReturn = queue[0];
queue.RemoveAt(0);
}
return toReturn;
}
这个块有return语句在锁
public DownloadFile Dequeue()
{
lock (QueueModifierLockObject)
{
DownloadFile toReturn = queue[0];
queue.RemoveAt(0);
return toReturn;
}
}
代码有什么不同吗?我知道性能差异(如果有的话)会很小,但我特别想知道lock 的发布顺序是否会有所不同。
【问题讨论】:
-
肯定这已经被覆盖了?
-
我找不到结果,因为我之前没有使用正确的搜索词。是的,这是一个骗局。