我正在编写一个应用程序,该应用程序在某些时候采用网格体并计算邻接索引。为此,我定义了一个 ConcurrentBag 对象数组,然后,在并行 for 循环中,我只检查一些面,如果它们有任何邻接,我将索引添加到相应索引中的所述包中。 IE。:
private bool parallelize = true;
private volatile ConcurrentBag<int>[] edge_adjacencies;
if (parallelize)
{
...
Parallel.For(0, face_count, compute_adjacency_single);
...
}
private void compute_adjacency_single(int cur_idx)
{
edge_adjacencies[cur_idx] = new ConcurrentBag<int>();
foreach(int test_idx in SOME_TEST_SPACE)
{
if (test_idx != cur_idx)
{
bool edge_adj, vertex_adj;
get_adjacency(cur_idx, test_idx, out edge_adj, out vertex_adj);
if (edge_adj && !collection_contains(edge_adjacencies[cur_idx], test_idx))
{
edge_adjacencies[cur_idx].Add(test_idx);
}
}
}
}
然后我对集合进行索引并检查每个集合的大小是否为 3(它们的大小都应该正好为 3):
//DEBUGGING
for (int i = 0; i < face_count; i++)
{
ConcurrentBag<int> cur = edge_adjacencies[i];
if (cur.Count != 3) Console.WriteLine("incorrect:" + i);
}
//DEBUGGING
这个过程的结果是不可预测的:有时我根本没有输出(所有大小都是 3),有时我得到不正确的输出:
运行 1:
incorrect:3791
incorrect:3792
incorrect:3829
incorrect:3837
incorrect:4476
运行 5:
incorrect:2855
incorrect:2856
incorrect:2879
incorrect:2880
运行 8:
incorrect:3271
每运行 9 次左右就会给出错误的结果。
作为引用,当我串行运行时,它每次都能完美运行。
我阅读了 MS 文档,它确实说 System.Collections.Concurrent 中的集合应该是线程安全的,但似乎并非如此。
为什么会发生这种情况,有没有好的方法可以防止这种情况发生?
最佳答案
唔。这是一个猜测 - 但我不认为这样做 edge_adjacencies[cur_idx] = ..
在 compute_adjacency_single
是线程安全的。CuncurrentBag<int>
是非常线程安全的,但保存它们实例的数组不是。
我会使用 ConcurrentDictonary而不是数组。
关于c# - Parallel.For 和 ConcurrentBag 给出不可预测的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58612830/