首页 > 其他 > 详细

Inside the Concurrent Collections: ConcurrentDictionary

时间:2020-05-11 14:18:46      阅读:48      评论:0      收藏:0      [点我收藏+]

When Iterating Over ConcurrentDictionary and only reading, is ConcurrentDictionary locked?

https://referencesource.microsoft.com/#mscorlib/system/Collections/Concurrent/ConcurrentDictionary.cs,785

This is how ConcurrentDictionary.GetEnumerator Is implemented:

/// <remarks>
/// The enumerator returned from the dictionary is safe to use concurrently with
/// reads and writes to the dictionary, however it does not represent a moment-in-time 
/// snapshot of the dictionary. The contents exposed through the enumerator may contain 
/// modifications made to the dictionary after <see cref="GetEnumerator"/> was called.
/// </remarks>
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
    Node[] buckets = m_tables.m_buckets;

    for (int i = 0; i < buckets.Length; i++)
    {
        // The Volatile.Read ensures that the load of the fields of ‘current‘
        // doesn‘t move before the load from buckets[i].
        Node current = Volatile.Read<Node>(ref buckets[i]);

        while (current != null)
        {
            yield return new KeyValuePair<TKey, TValue>(current.m_key, current.m_value);
            current = current.m_next;
        }
    }
}

As you see, the iteration is lock free, and simply yields a immutable struct (KeyValuePair) which is returned to the caller for each iteration. That is why it cant guarantee a snapshot-in-time of the ConcurrentDictionary

This will definitely not have a performance effect on adding/updating new values while iterating, but it simply cant guarantee that your admin will see the most updated snapshot of the dictionary.

  1. You can browse the rest of the source code yourself via http://sourceof.net
  2. And you can also check out Inside the Concurrent Collections: ConcurrentDictionary by Simon Cooper.
  3. Are all of the new concurrent collections lock-free?

Inside the Concurrent Collections: ConcurrentDictionary

 

Inside the Concurrent Collections: ConcurrentDictionary

原文:https://www.cnblogs.com/chucklu/p/12868847.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!