Partition は私が作成した小さなクラスです。Partitions という名前の ConcurrentDictionary に存在する何千ものパーティションがあります。シリアル化の前に、特定のパーティションをロックし、いくつかの作業を行ってから、パーティションのロックを解除したいと考えています。
この間、他のスレッドはそのパーティションにアクセスできません。私のクラス Partition は参照型であるため、以下のコードのように単一の Partition オブジェクトをロックできるかどうか疑問に思っています。
これにより、以下のロック中に ConcurrentDictionary パーティション内の単一のパーティションからの他のスレッドがブロックされますか?
クラス レベルのロッカー オブジェクトを使用してロックしたくありません。私の考えでは、複数のスレッドが以下のコードを同時に実行できるようにする必要があります。ロックされている場合、特定のパーティションにアクセスできないはずです...
private void serializePartition(int partNo)
{
Partition p;
//Get a reference to the partition, lock, and save it to disk.
lock (p = Partitions[partNo])
{
using (var file = File.Create(dataPath + tableName + partNo + ".ppmi"))
{
Serializer.Serialize(file, p);
}
//decrement _CurrentRecordsInMemory by the size of the partition.
Interlocked.Add(ref _CurrentRecordsInMemory, p.Data.Count * -1);
//Clear the partitions data to free up memory and reset partition variables
p.Data = null;
p.OnDisk = true;
p.StartCount = 0;
}
}