简体   繁体   English

使用现有对象而不是创建特定锁对象是否安全?

[英]Is using an existing object rather than creating a specific lock object safe?

EDIT: As it turns out when I was browsing I found a question the appears to be the same as mine which I didn't find earlier: Difference between lock(locker) and lock(variable_which_I_am_using) 编辑:事实证明,当我浏览时,我发现了一个与我之前没有找到的问题相同的问题: 锁定(锁定器)和锁定之间的区别(variable_which_I_am_using)

I am looking at some code and trying to get my head around the locking thing and I am getting there I think. 我正在查看一些代码并试图了解锁定的事情,我想到了。

Now I noticed in some code I am reviewing that an object is created like so: 现在我注意到在一些代码中我正在审查一个对象是这样创建的:

private HashSet<Graphic> clustersInUse = new HashSet<Graphic>();

Then further in the code is used like so: 然后在代码中进一步使用如下:

lock (clustersInUse)
{
   // Do something with the Hashset
}

Now, is there a problem doing this rather than creating a specific object for the lock. 现在,这样做是否存在问题,而不是为锁创建特定对象。 Like this: 像这样:

private object clusterLocker = new object();

What happens if the clustersInUse above somehow gets put into a public property, what happens then? 如果上面的clustersInUse以某种方式被置于公共财产中,会发生什么呢?

Also, if something tries to access the clustersInUse without locking it whilst it is locked in another thread what would happen then? 此外,如果某些东西试图访问clustersInUse而没有锁定它同时它被锁定在另一个线程中会发生什么呢?

The general rule is that you want to control the scope of the object your locking on to prevent some unknown code from causing unexpected behavior. 一般规则是您希望控制锁定对象的范围,以防止某些未知代码导致意外行为。 In this case you are using a private instance variable so you are probably OK as long as you are not handing out references to it. 在这种情况下,您正在使用私有实例变量,因此只要您没有分发对它的引用,您就可以了。

If you are handing out references and locking on it and other code is locking those references (eg when modifying the collection) changing the behavior could easily introduce threading bugs. 如果您正在分发引用并锁定它,而其他代码正在锁定这些引用(例如,在修改集合时),更改行为可能很容易引入线程错误。

If someone puts it into a public property that counts as "handing out references" if they lock on it your call to lock will block until they unlock it. 如果有人把它放入一个公共财产,如果他们锁定它就算作“分发参考”,你的锁定电话将被阻止,直到他们解锁它为止。 Whether this is desirable or not depends on what they are doing with the collection. 这是否可取取决于他们对收藏品的处理方式。

Having the object locked will have have no effect on using the object for any purpose other than synchronization. 锁定对象对于将对象用于除同步之外的任何目的没有任何影响。

You've pretty much answered your own question. 你几乎回答了自己的问题。 For locking, it's generally better to create an object specifically for the purpose, and usually held privately for use by accessor methods that express synchronisation logic at a high level. 对于锁定,通常最好是专门为此目的创建一个对象,并且通常私有地保存以供高级别表示同步逻辑的访问器方法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅锁定集合中的对象而不锁定整个集合是否安全? - Is it safe to lock just the object in the collection rather than locking the whole collection? 使用LINQ解析XML并填充现有对象的属性,而不是创建一个新的属性 - Parse XML using LINQ and fill existing object's properties rather than creating a new one .NET MVC视图返回对象名称而不是特定对象 - .NET MVC view returning object name rather than specific object 使用对象以外的东西锁定线程 - Lock thread using something other than an object 创建新的复杂对象; EF尝试创建新的相关对象而不是关联对象 - Creating new Complex Object; EF tries to create new related object rather than associate 多态-使用从接口继承而不是直接从接口继承的对象 - Polymorphism - Using an object that inherits from an interface rather than the interface directly Dapper 使用第二个 ID 列而不是第一个列映射 object - Dapper maps object using second ID column rather than first 您可以使用Dapper刷新现有的对象引用而不是总是返回新的对象引用吗? - Can you use Dapper to refresh existing object references rather than always returning new ones? 使用现有查询而不是SELECT语句 - Using an existing query rather than a SELECT statement 对象池似乎正在返回对象副本,而不是对象引用 - Object pool seems to be returning an object copy rather than an object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM