1

「スレッドを保存」しない辞書の動作を再現して、サンプルを実装しようとしました (以下を参照)。デッドロックを予期していましたが、テストは問題なく機能しました。私のテストの問題と、マルチスレッド辞書エラーをシミュレートする方法を教えてください。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace parallelTest
{
    [TestClass]
    public class UnitTest1
    {
        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        [TestMethod]
        public void TestMethod1()
        {
            dictionary[2000] = "test";

            Parallel.For(0, 1000, i =>
            {
                string value;
                dictionary.TryGetValue(2000, out value);
                dictionary[2000] = String.Format("new value {0}", i);
                dictionary.Add(i, String.Format("{0}", i));
                Trace.WriteLine(String.Format("thread: {0}, {1}, {2}", Thread.CurrentThread.ManagedThreadId, i, value));
                Thread.Sleep(100);
            }
            );
        }
    }
}
4

1 に答える 1

1

Dictionaryは内部データをロックしないため、デッドロックに直面することはありません。
ただし、辞書に予期しないデータが含まれる可能性があります。この場合: 追加されなかったアイテム。

ストレステスト後に、ディクショナリが期待どおりであるかどうかを確認する必要があります。

for(Int32  index=0; index < 1000; index++)
{
    if(dictionary.Values.Any(index.ToString()) ==  false)
    {
          // problem
    }

}

ちなみに、辞書にストレスを与えたい場合は、関数内の他のすべての操作 (String.Format など) を削除する必要があります。そうすることで、辞書に問題を作成する機会が増えます。

これは public add メソッドを呼び出したときに呼び出される内部メソッドであり、ご覧のとおり、そこにはロックがありません。

private void Insert(TKey key, TValue value, bool add)
{
    if (key == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
    }
    if (this.buckets == null)
    {
        this.Initialize(0);
    }
    int num = this.comparer.GetHashCode(key) & 2147483647;
    int num2 = num % this.buckets.Length;
    int num3 = 0;
    for (int i = this.buckets[num2]; i >= 0; i = this.entries[i].next)
    {
        if (this.entries[i].hashCode == num && this.comparer.Equals( this.entries[i].key
                                                                   , key))
        {
            if (add)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource
                                                      .Argument_AddingDuplicate);
            }
            this.entries[i].value = value;
            this.version++;
            return;
        }
        num3++;
    }
    int num4;
    if (this.freeCount > 0)
    {
        num4 = this.freeList;
        this.freeList = this.entries[num4].next;
        this.freeCount--;
    }
    else
    {
        if (this.count == this.entries.Length)
        {
            this.Resize();
            num2 = num % this.buckets.Length;
        }
        num4 = this.count;
        this.count++;
    }
    this.entries[num4].hashCode = num;
    this.entries[num4].next = this.buckets[num2];
    this.entries[num4].key = key;
    this.entries[num4].value = value;
    this.buckets[num2] = num4;
    this.version++;
    if (num3 > 100 && HashHelpers.IsWellKnownEqualityComparer(this.comparer))
    {
        this.comparer = (IEqualityComparer<TKey>)HashHelpers
                                    .GetRandomizedEqualityComparer(this.comparer);
        this.Resize(this.entries.Length, true);
    }
}

お役に立てば幸いです。

于 2013-08-12T07:31:53.570 に答える