gpt4 book ai didi

c# - 线程安全的 SortedDictionary

转载 作者:太空狗 更新时间:2023-10-29 21:50:17 26 4
gpt4 key购买 nike

我创建了一个使用 SortedDictionary 来存储和操作数据的类。该类在多线程环境中实现时效果很好。现在,我想通过为内部 SortedDictionary 类编写一个包装类来使类线程安全。我想使用 Reader-Writer Locks 来实现这个,但是现在,我在编写包装类本身时遇到了问题。具体来说,我不确定如何为字典实现 Enumerator。这是我现在的类(class)的完整代码。

    public class ConcurrentSortedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
{
#region Variables
SortedDictionary<TKey, TValue> _dict;
#endregion
#region Constructors
public ConcurrentSortedDictionary()
{
_dict = new SortedDictionary<TKey, TValue>();
}

public ConcurrentSortedDictionary(IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(comparer);
}

public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary);
}

public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary, comparer);
}
#endregion
#region Properties
public IComparer<TKey> Comparer
{
get
{
return _dict.Comparer;
}
}

public int Count
{
get
{
return _dict.Count;
}
}

public TValue this[TKey key]
{
get
{
return _dict[key];
}

set
{
_dict[key] = value;
}
}

public SortedDictionary<TKey, TValue>.KeyCollection Keys
{
get
{
return new SortedDictionary<TKey,TValue>.KeyCollection(_dict);
}
}

public SortedDictionary<TKey, TValue>.ValueCollection Values
{
get
{
return new SortedDictionary<TKey, TValue>.ValueCollection(_dict);
}
}

#endregion
#region Methods
public void Add(TKey key, TValue value)
{
_dict.Add(key, value);
}

public void Clear()
{
_dict.Clear();
}

public bool ContainsKey(TKey key)
{
return _dict.ContainsKey(key);
}

public bool ContainsValue(TValue value)
{
return _dict.ContainsValue(value);
}

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
{
_dict.CopyTo(array, index);
}

public override bool Equals(Object obj)
{
return _dict.Equals(obj);
}

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return GetEnumerator();
}

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}

public override int GetHashCode()
{
return _dict.GetHashCode();
}

public bool Remove(TKey key)
{
return _dict.Remove(key);
}

public override string ToString()
{
return _dict.ToString();
}

public bool TryGetValue(TKey key, out TValue value)
{
return _dict.TryGetValue(key, out value);
}
#endregion
}

当我编译代码时,我收到错误消息:

'ConcurrentSortedDictionary' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'ConcurrentSortedDictionary.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'.

我在这里查看了几篇与此相关的帖子作为引用:

How do I implement IEnumerable in my Dictionary wrapper class that implements IEnumerable<Foo>? What's the best way of implementing a thread-safe Dictionary?

但我看不出我做错了什么。非常感谢任何帮助。

最佳答案

问题出在这里:

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return GetEnumerator();
}

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}

你需要:

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return _dict.GetEnumerator();
}

第二个非泛型 GetEnumerator() 是一个 explicit interface implementation并且需要作为对 C# 中存在泛型和泛型集合之前的日子的不幸倒退。

另请参阅:IEnumerable<T> provides two GetEnumerator methods - what is the difference between them? (特别是 Michael B's answer )。

但是如果您希望枚举与您的类的其余部分一起是线程安全的,您可能还需要编写自己的线程安全IEnumerator 类型来配合在您的类(class)中使用读者/作者锁!

关于c# - 线程安全的 SortedDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22308067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com