コレクションのコレクションを作成する必要があります。コレクションは複数のスレッドによって呼び出され、アイテムとルックアップ アイテムを追加します。追加されたアイテムは削除されません。現在、要素を追加している間、コレクション全体をロックする必要があります。ロックフリーにする方法はありますか?または、使用できるより良いデータ構造またはパターンはありますか? これが私のコードの簡略版です:
readonly ConcurrentDictionary<string, ConcurrentDictionary<int, int>> dict = new ConcurrentDictionary<string, ConcurrentDictionary<int, int>>();
void AddUpdateItem(string s, int k, int v)
{
ConcurrentDictionary<int, int> subDict;
if (dict.TryGetValue(s, out subDict))
{
subDict[k] = v;
}
else
{
lock (dict)
{
if (dict.TryGetValue(s, out subDict))
{
subDict[k] = v;
}
else
{
subDict = new ConcurrentDictionary<int, int>();
subDict[k] = v;
dict[s] = subDict;
}
}
}
}