Can I put a return statement inside a lock

后端 未结 3 1486
温柔的废话
温柔的废话 2021-01-01 20:07

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

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 20:42

    Yes, but why not use Dequeue?

    Remember lock is simply shorthand for essentially something along the lines of:

            try
            {
                Monitor.Enter(QueueModifierLockObject);
    
                DownloadFile toReturn = queue.Dequeue();         
    
                return toReturn;
            }
            finally
            {
                Monitor.Exit(QueueModifierLockObject);
            }
    

提交回复
热议问题