Why should we avoid blocking (this)?

Possible duplicate:
Why is lock (this) {...} bad?

In C #, to make a critical area stream safe, we can use an operator lock(). The lock statement takes an object. What is wrong if we pass thislocks to the statement?

+3
source share
2 answers

From http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx :

In general, avoid blocking the public type or instances that go beyond your code. control. General designs lock (this), lock (typeof (MyType))and lock ("myLock")violate this manual:

  • lock (this) , .
  • lock (typeof (MyType)) - , MyType .
  • lock("myLock") - , , , .

, .

+6

this , , . , this, . , , , , .

+6

Source: https://habr.com/ru/post/1765339/


All Articles