Lock on an object that might change during code execution

情到浓时终转凉″ 提交于 2020-02-21 13:02:41

问题


Let's suppose I have a thread that locks on an object reference

Thread #1

lock(myObj) { ... }

later in code I have myObj = new XYZObj();

and then Thread #2 locks on it

lock(myObj) { ... }

Will this code be thread safe, if the object reference has changed? When the object reference changes, the first lock is still valid?


回答1:


Locks work on instances, not variables.
The lock statement will hold its own reference to the instance so that it will only exit the instance you entered.

The spec says:

where x is an expression of a reference-type, is precisely equivalent to

System.Threading.Monitor.Enter(x);
try {
   ...
}
finally {
   System.Threading.Monitor.Exit(x);
}

except that x is only evaluated once.

If you re-assign the variable between the two locks, you will get two valid locks on two different instances.

In general, however, you should never do that; it's a recipe for subtle bugs and race conditions.
You should only lock on dedicated readonly lock objects.




回答2:


No. They will both be locking on different objects.

According to MSDN

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.




回答3:


Will this code be thread safe

The statement lock(myObj) { ... } is only safe until a new object reference is assigned to the myObj variable. Addition: Also, it's only safe if any data shared between threads that is used non-atomically mutating inside a lock on an object is only used non-atomically mutating inside locks on that same object.

So, every time you enter a lock for myObj, the actual referenced object is what is being used for the lock, not your variable. If you change the variable to reference a new object, then you're effectively locking different objects in different locks, which obviously isn't what you wanted. But, then again, the next time you come back to the first lock, the first and second lock object might be in sync again, and so it'll be safe again. Maybe!

As you can see, that behavior is completely broken. Is this a hypothetical question or are you really doing like that?



来源:https://stackoverflow.com/questions/8578870/lock-on-an-object-that-might-change-during-code-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!