Should a return statement be inside or outside a lock?

后端 未结 9 2149
别那么骄傲
别那么骄傲 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条回答
  •  死守一世寂寞
    2020-12-12 19:08

    I would definitely put the return inside the lock. Otherwise you risk another thread entering the lock and modifying your variable before the return statement, therefore making the original caller receive a different value than expected.

提交回复
热议问题