Block .Net - link blocked or object?

Let's say I have this code:

object o1 = new Object(); object o2 = o1; 

Is locking at o1 the same as getting locking at o2? (If o1 is blocked, does block o2 block until o1 is released?)

+4
source share
4 answers

If he blocked the link, blocking will be useless. The problem is that the links themselves are copied by value, so you should always block a temporary copy that is immediately discarded.

So it’s not how it works. The lock is obtained on the instance that refers to the link, not the link.

+8
source

Gets a lock on o1 just like getting a lock on o2 ?

Yes.

It works with something called sync-block , which is part of every instance of the object. But functionally you can think of it as how to use an object as a key in a dictionary.

Link locking will be the same as value type locking , with the same problems.

+5
source

Yes, since locking is performed on an object, not a reference to an object. o2 = o1 copies the link, not the object.

+2
source

Yes, .NET locks on links (locking on value type will result in an error)

0
source

All Articles