gpt4 book ai didi

c# - 自更新集合并发问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:08 25 4
gpt4 key购买 nike

我正在尝试构建一个 self 更新的集合。集合中的每个项目都有一个位置 (x,y)。当位置改变时,会触发一个事件,并且集合会重新定位该项目。

集合内部使用的是“锯齿状字典”。外部字典使用 x 坐标 a 键,而嵌套字典使用 y 坐标 a 键。嵌套字典然后将项目列表作为值。

该集合还维护一个字典来存储项目在嵌套字典中的位置——项目到存储位置的查找。

我在确保收集线程安全方面遇到了一些问题,而这正是我真正需要的。

集合的源代码:

    public class PositionCollection<TItem, TCoordinate> : ICollection<TItem>
where TItem : IPositionable<TCoordinate>
where TCoordinate : struct, IConvertible
{
private readonly object itemsLock = new object();
private readonly Dictionary<TCoordinate, Dictionary<TCoordinate, List<TItem>>> items;
private readonly Dictionary<TItem, Vector<TCoordinate>> storedPositionLookup;

public PositionCollection()
{
this.items = new Dictionary<TCoordinate, Dictionary<TCoordinate, List<TItem>>>();
this.storedPositionLookup = new Dictionary<TItem, Vector<TCoordinate>>();
}

public void Add(TItem item)
{
if (item.Position == null)
{
throw new ArgumentException("Item must have a valid position.");
}

lock (this.itemsLock)
{
if (!this.items.ContainsKey(item.Position.X))
{
this.items.Add(item.Position.X, new Dictionary<TCoordinate, List<TItem>>());
}

Dictionary<TCoordinate, List<TItem>> xRow = this.items[item.Position.X];

if (!xRow.ContainsKey(item.Position.Y))
{
xRow.Add(item.Position.Y, new List<TItem>());
}

xRow[item.Position.Y].Add(item);

if (this.storedPositionLookup.ContainsKey(item))
{
this.storedPositionLookup[item] = new Vector<TCoordinate>(item.Position);
}
else
{
this.storedPositionLookup.Add(item, new Vector<TCoordinate>(item.Position)); // Store a copy of the original position
}

item.Position.PropertyChanged += (object sender, PropertyChangedEventArgs eventArgs) => this.UpdatePosition(item, eventArgs.PropertyName);
}
}

private void UpdatePosition(TItem item, string propertyName)
{
lock (this.itemsLock)
{
Vector<TCoordinate> storedPosition = this.storedPositionLookup[item];
this.RemoveAt(storedPosition, item);
this.storedPositionLookup.Remove(item);
}

this.Add(item);

}
}

我编写了一个简单的单元测试来检查并发问题:

        [TestMethod]
public void TestThreadedPositionChange()
{
PositionCollection<Crate, int> collection = new PositionCollection<Crate, int>();
Crate crate = new Crate(new Vector<int>(5, 5));
collection.Add(crate);

Parallel.For(0, 100, new Action<int>((i) => crate.Position.X += 1));

Crate same = collection[105, 5].First();
Assert.AreEqual(crate, same);
}

每次运行测试时,实际存储的位置都不同。感谢您提供任何反馈。

最佳答案

您可能会为每个 X 坐标维护一个锁列表以提高并发性,否则您不会看到太多性能提升。

或者,您可以切换到四叉树或其他一些空间索引系统,以便在项目移动时最大限度地减少数据结构的扰动。使用四叉树,您可以通过在各个树级别而不是数据结构范围内持有独立锁来最大限度地减少并发问题。

您的数据结构使得跟踪移动物体的成本非常高,因为它必须几乎不断地 self 更新。使用“分桶”方法(如果您对 X/Y 坐标有限制),您可以将更新限制为仅在项目更改分桶时进行。

private void UpdatePosition(TItem item)
{
// each bucket holds some MxN section of the X,Y grid
var nextBucket = CalculateBucket(item.Position);
if (nextBucket != item.Bucket)
{
lock (wholeCollectionLock)
{
this.buckets[nextBucket].Add(item);
this.buckets[item.Bucket].Remove(item);
item.Bucket = nextBucket;
}
}
}

public IEnumerable<TItem> ItemsAt(TPosition position)
{
var bucket = CalculateBucket(position);
lock (wholeCollectionLock)
{
// this will be efficient enough for cases where O(n) searches
// have small enough n's
// needs to be .ToArray for "snapshot"
return this.buckets[bucket].Where(xx => xx.Position == position).ToArray();
}
}

关于c# - 自更新集合并发问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2852724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com