在字典&LT的重点用锁;串,对象> [英] Using lock on the key of a Dictionary<string, object>

查看:172
本文介绍了在字典&LT的重点用锁;串,对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个词典<字符串,someobject>



编辑:有人向我指出,我的例子是坏的。我的整个意图不是更新循环引用,但是,线程不同的充需要更新/获取数据更新基于不同的价值观。我改变了循环的方法



我需要更新我的字典项目 - 一次一个关键,如果有使用锁的问题,我想知道?在我的词典对象的价值。关键

 私有静态字典<字符串,MatrixElement> matrixElements =新词典<字符串,MatrixElement>(); 

//伪代码

公共静态无效UpdateValue(字符串键)
{
KeyValuePair<字符串,MatrixElement> keyValuePair = matrixElements [关键]
锁(keyValuePair.Key)
{
keyValuePair.Value = SomeMeanMethod();
}
}



那会托起在法庭上还是失败?我只是想在字典中的每个值被independantly锁定,锁定(和更新)一个值不锁等。另外我知道的锁定将举行很长一段时间 - 但数据是无效的,直到完全更新


解决方案

锁定的对象是代码锁定它的外部访问的是一个很大的风险。如果任何其他代码(任意位置)以往任何时候都反对,你可以锁定一些死锁是很难调试是英寸另外请注意,你锁定的对象的,没有参照,所以如果我给你一本字典,我可能仍然抱到键引用和他们锁定 - 导致我们锁定同一对象上<。 / p>

如果的你完全封装的字典,生成密钥自己(他们通常不会传递,那么你可能是安全的。



不过,尝试坚持一个规则 - 限制你锁定在锁定代码本身尽可能


$ b $对象的可见性。 b

这就是为什么你看到这一点:

 公共类的东西
{
私人只读对象lockObj =新的对象();

公共SomethingReentrant()
{
锁(lockObj)// A线
{
// ...
}
}
}

,而不是看到上述取代A线通过

 锁(本)

这样,一个单独的对象被锁定,并且能见度有限。



修改乔恩斯基特正确地指出lockObj上面应该是只读的。


I have a Dictionary<string, someobject>.

EDIT: It was pointed out to me, that my example was bad. My whole intention was not to update the references in a loop but to update different values based on differnt threads need to update/get the data. I changed the loop to a method.

I need to update items in my dictionary - one key at a time and i was wondering if there are any problems in using the lock on the .key value of my Dictionary object?

private static Dictionary<string, MatrixElement> matrixElements = new Dictionary<string, MatrixElement>();

//Pseudo-code

public static void UpdateValue(string key)
{
    KeyValuePair<string, MatrixElement> keyValuePair = matrixElements[key];
    lock (keyValuePair.Key)
    {
        keyValuePair.Value  = SomeMeanMethod();
    }
}

Would that hold up in court or fail? I just want each value in the dictionary to be locked independantly so locking (and updating) one value does not lock the others. Also i'm aware the locking will be holding for a long time - but the data will be invalid untill updated fully.

解决方案

Locking on an object that is accessible outside of the code locking it is a big risk. If any other code (anywhere) ever locks that object you could be in for some deadlocks that are hard to debug. Also note that you lock the object, not the reference, so if I gave you a dictionary, I may still hold references to the keys and lock on them - causing us to lock on the same object.

If you completely encapsulate the dictionary, and generate the keys yourself (they aren't ever passed in, then you may be safe.

However, try to stick to one rule - limit the visibility of the objects you lock on to the locking code itself whenever possible.

That's why you see this:

public class Something
{
  private readonly object lockObj = new object();

  public SomethingReentrant()
  {
    lock(lockObj)    // Line A
    {
      // ...
     }
   }
}

rather than seeing line A above replaced by

  lock(this)

That way, a separate object is locked on, and the visibility is limited.

Edit Jon Skeet correctly observed that lockObj above should be readonly.

这篇关于在字典&LT的重点用锁;串,对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆