任何缺点锁定集合与一个SyncRoot上? [英] Any downsides to locking a collection vs. a syncRoot?

查看:88
本文介绍了任何缺点锁定集合与一个SyncRoot上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有任何缺点的集合锁定在如名单,其中,T> 的HashSet< T> 词典< TKEY的,TValue> ,而不是一个简单的对象

请注意:在下面的实施例,也就是其中所述锁发生的唯一场所,它没有被锁定的从多个地方,但静态方法可以由多个线程调用。此外, _dict 是从来没有的 GetSomething 方法之外访问。

我目前的code是这样的:

 私有静态只读字典<字符串,字符串> _dict =新字典<字符串,字符串>();
公共静态字符串GetSomething(字符串键)
{
    字符串结果;
    如果(!_dict.TryGetValue(键,出来的结果))
    {
        锁(_dict)
        {
            如果(!_dict.TryGetValue(键,出来的结果))
            {
                _dict [关键] =结果= CalculateSomethingExpensive(密钥);
            }
        }
    }
    返回结果;
}
 

另一名开发者告诉我,在锁定的集合会导致一些问题,但我怀疑。请问我的code更有效的,如果我这样做这样?

 私有静态只读字典<字符串,字符串> _dict =新字典<字符串,字符串>();
私人静态只读对象_syncRoot =新的对象();
公共静态字符串GetSomething(字符串键)
{
    字符串结果;
    如果(!_dict.TryGetValue(键,出来的结果))
    {
        锁定(_syncRoot)
        {
            如果(!_dict.TryGetValue(键,出来的结果))
            {
                _dict [关键] =结果= CalculateSomethingExpensive(密钥);
            }
        }
    }
    返回结果;
}
 

解决方案

如果你暴露你收藏到外面的世界,那么,是的,这可能是一个问题。通常的建议是,在你完全属于自己的东西锁,并且不能被意外地由code这是你的影响之外锁定。这就是为什么通常它可能会更好的东西,你甚至从来没有考虑暴露(即为此目的而设立的特定的锁对象)锁定。这样,当你的内存失败,你,你会<打击>从来没有可能不会得到意想不到的收获。

要更直接地回答你的问题:添加其他对象到组合是永远不会更有效率,但把什么通常被认为是良好的编码习惯有些人认为过,但不可测的效率可能是pmaturely发生$ P $的optmisation 。我喜欢的最佳实践,直到它的demonstrably造成了瓶颈。

I'm wondering if there are any downsides to locking over a collection such as a List<T>, HashSet<T>, or a Dictionary<TKey, TValue> rather than a simple object.

Note: in the following examples, that is the only place where the locks occur, it's not being locked from multiple places, but the static method may be called from multiple threads. Also, the _dict is never accessed outside of the GetSomething method.

My current code looks like this:

private static readonly Dictionary<string, string> _dict = new Dictionary<string, string>();
public static string GetSomething(string key)
{
    string result;
    if (!_dict.TryGetValue(key, out result))
    {
        lock (_dict)
        {
            if (!_dict.TryGetValue(key, out result))
            {
                _dict[key] = result = CalculateSomethingExpensive(key);
            }
        }
    }
    return result;
}

Another developer is telling me that locking on a collection will cause issues, but I'm skeptical. Would my code be more efficient if I do it this way?

private static readonly Dictionary<string, string> _dict = new Dictionary<string, string>();
private static readonly object _syncRoot = new object();
public static string GetSomething(string key)
{
    string result;
    if (!_dict.TryGetValue(key, out result))
    {
        lock (_syncRoot)
        {
            if (!_dict.TryGetValue(key, out result))
            {
                _dict[key] = result = CalculateSomethingExpensive(key);
            }
        }
    }
    return result;
}

解决方案

If you expose your collections to the outside world, then, yes this can be a problem. The usual recommendation is to lock on something that you exclusively own and that can never be locked unexpectedly by code that is outside your influence. That's why generally it's probably better to lock on something that you'd never even consider exposing (i.e. a specific lock object created for that purpose). That way, when your memory fails you, you'll never probably not get unexpected results.

To answer your question more directly: Adding another object into the mix is never going to be more efficient, but placing what is generally regarded as good coding practice before some perceived, but unmeasured efficiency might be an optmisation occurring prematurely. I favour best practice until it's demonstrably causing a bottleneck.

这篇关于任何缺点锁定集合与一个SyncRoot上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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