我遇到一些涉及Parallel for循环并添加到列表的问题。问题是,相同的代码可能在不同的时间产生不同的输出。我已经在下面设置了一些测试代码。在这段代码中,我创建了一个包含10,000个int值的列表。 1/10的值将为0,1/10的值将为1,一直到1/10的值为9.并行For循环 - 添加到列表时出现问题
设置此列表后,我设置了一个Parallel for循环遍历列表。如果当前的数字是0,我将一个值添加到一个新的列表。 Parallel for循环完成后,我输出列表的大小。尺寸应该始终为1,000。大多数时候,答案都是正确的。然而,我看到3点可能的不正确的结果发生:
- 列表的大小小于1,000
- 一种IndexOutOfRangeException发生@
doubleList.Add(0.0);
- 一种ArgumentException发生@
doubleList.Add(0.0);
该消息给出的ArgumentException是:Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
什么可能导致错误S'这是.Net错误吗?有什么我可以做,以防止这种情况发生?
请为自己尝试一下。如果您没有收到错误,请尝试几次。另请注意,使用单核机器可能不会看到任何错误。
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ParallelTest
{
class Program
{
static void Main(string[] args)
{
List<int> intList = new List<int>();
List<double> doubleList = new List<double>();
for (int i = 0; i < 250; i++)
{
intList.Clear();
doubleList.Clear();
for (int j = 0; j < 10000; j++)
{
intList.Add(j % 10);
}
Parallel.For(0, intList.Count, j =>
{
if (intList[j] == 0)
{
doubleList.Add(0.0);
}
});
if (doubleList.Count != 1000)
{
Console.WriteLine("On iteration " + i + ": List size = " + doubleList.Count);
}
}
Console.WriteLine("\nPress any key to exit.");
Console.ReadKey();
}
}
}
什么更可能是.NET错误或代码中的错误? – 2010-05-02 03:50:30
调试规则1:错误总是在你的代码中。你可以从* title *这个... – Aaronaught 2010-05-02 03:58:20