スレッドセキュリティのリストセット(パフォーマンスが悪い..

18066 ワード

MSはListのスレッドセキュリティのセットを提供していないためである.自分で1つ書きましたが、性能は...高くありません.性能に対する要求が高い場合に用いることができる.using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace System.Collections.Concurrent { /// /// List /// /// public class ConcurrentList : IList { /// /// List /// public ConcurrentList() { list = new List(); } /// /// /// protected List list; /// /// /// protected int lockInt; #region IList /// /// , System.Collections.Generic.List /// /// /// public int IndexOf(T item) { bool gotLock = false; try { splock.Enter(ref gotLock); return list.IndexOf(item); } finally { if (gotLock) { splock.Exit(); } } } /// /// System.Collections.Generic.List /// /// /// public void Insert(int index, T item) { bool gotLock = false; try { splock.Enter(ref gotLock); list.Insert(index, item); } finally { if (gotLock) { splock.Exit(); } } } /// /// System.Collections.Generic.List /// /// public void RemoveAt(int index) { bool gotLock = false; try { splock.Enter(ref gotLock); list.RemoveAt(index); } finally { if (gotLock) { splock.Exit(); } } } public T this[int index] { get { System.Threading.Interlocked.Increment(ref lockInt); try { return list[index]; } finally { System.Threading.Interlocked.Decrement(ref lockInt); } } set { System.Threading.Interlocked.Increment(ref lockInt); try { list[index] = value; } finally { System.Threading.Interlocked.Decrement(ref lockInt); } } } public void Add(T item) { bool gotLock = false; try { splock.Enter(ref gotLock); list.Add(item); } finally { if (gotLock) { splock.Exit(); } } } public void Clear() { bool gotLock = false; try { splock.Enter(ref gotLock); list.Clear(); } finally { if (gotLock) { splock.Exit(); } } } public bool Contains(T item) { bool gotLock = false; try { splock.Enter(ref gotLock); return list.Contains(item); } finally { if (gotLock) { splock.Exit(); } } } public void CopyTo(T[] array, int arrayIndex) { bool gotLock = false; try { splock.Enter(ref gotLock); list.CopyTo(array, arrayIndex); } finally { if (gotLock) { splock.Exit(); } } } public int Count { get { System.Threading.Interlocked.Increment(ref lockInt); try { return list.Count; } finally { System.Threading.Interlocked.Decrement(ref lockInt); } } } public bool IsReadOnly { get { return false; } } /// /// System.Collections.Generic.List /// /// /// public bool Remove(T item) { bool gotLock = false; try { splock.Enter(ref gotLock); return list.Remove(item); } finally { if (gotLock) { splock.Exit(); } } } public IEnumerator GetEnumerator() { bool gotLock = false; try { splock.Enter(ref gotLock); return list.GetEnumerator(); } finally { if (gotLock) { splock.Exit(); } } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion /// /// /// System.Threading.SpinLock splock = new System.Threading.SpinLock(); /// /// System.Collections.Generic.List /// /// /// /// public List GetRange(int index, int count) { bool gotLock = false; try { splock.Enter(ref gotLock); return list.GetRange(index, count); } finally { if (gotLock) { splock.Exit(); } } } /// /// System.Collections.Generic.List /// /// /// public void RemoveRange(int index, int count) { bool gotLock = false; try { splock.Enter(ref gotLock); list.RemoveRange(index, count); } finally { if (gotLock) { splock.Exit(); } } } } }