Потокобезопасный SortedDictionary
Я сделал класс, который используетSortedDictionary
хранить и манипулировать данными. Класс отлично работает, за исключением случаев, когда он реализован в многопоточной среде. Теперь я хотел бы сделать поток класса безопасным, написав класс-оболочку для внутреннегоSortedDictionary
учебный класс. Я хотел бы использовать блокировки Reader-Writer для реализации этого, но сейчас у меня проблемы с написанием самого класса-оболочки. В частности, я не уверен, как реализоватьEnumerator
для словаря. Вот мой полный код для класса в его нынешнем виде.
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 не реализует элемент интерфейса System.Collections.IEnumerable.GetEnumerator (). «ConcurrentSortedDictionary.GetEnumerator ()» не может реализовать «System.Collections.IEnumerable.GetEnumerator ()», поскольку у него нет соответствующего возвращаемого типа «System.Collections.IEnumerator».
Я посмотрел несколько постов, связанных с этим, в качестве ссылки:
Как мне реализовать IEnumerable в моем классе-обертке Dictionary, который реализует IEnumerable <Foo>? Каков наилучший способ реализации многопоточного словаря?
Но я не вижу, что я делаю неправильно. Любая помощь с благодарностью.