Should a return statement be inside or outside a lock?

后端 未结 9 2155
别那么骄傲
别那么骄傲 2020-12-12 18:32

I just realized that in some place in my code I have the return statement inside the lock and sometime outside. Which one is the best?

1)

void exampl         


        
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 19:20

    For what it's worth, the documentation on MSDN has an example of returning from inside of the lock. From the other answers on here, it does appear to be pretty similar IL but, to me, it does seem safer to return from inside the lock because then you don't run the risk of a return variable being overwritten by another thread.

提交回复
热议问题