Dupe: return statement in a lock procedure: inside or outside
The title is a little misleading. I know that you can do it, but I'm wondering about the performance implications.
consider these two blocks of code. (no error handling)
This block has the return
outside of the lock
public DownloadFile Dequeue()
{
DownloadFile toReturn = null;
lock (QueueModifierLockObject)
{
toReturn = queue[0];
queue.RemoveAt(0);
}
return toReturn;
}
This block has the return
statement within the lock
public DownloadFile Dequeue()
{
lock (QueueModifierLockObject)
{
DownloadFile toReturn = queue[0];
queue.RemoveAt(0);
return toReturn;
}
}
Is there any difference in the code? I understand that the performance differences (if any) would be minimal, but I am specifically wondering if there would be a difference in the order that the lock
gets release.