Creating list of lists with Parallel.For - what am I doing wrong?
I'm trying to populate a list of lists within a Parallel.For loop, but
when the loop completes the list of lists is empty. What am I doing wrong?
int[] nums = Enumerable.Range(0, 10).ToArray();
IList<IList<double>> bins = new List<IList<double>>();
Parallel.For<IList<IList<double>>>(0, nums.Length, () => new
List<IList<double>>(), (i, loop, bin) =>
{
Random random = new Random();
IList<double> list = new List<double>();
for (int j = 0; j < 5; j++)
list.Add(random.NextDouble() + i);
bin.Add(list);
return bin;
}
,
(bin) =>
{
lock (bins)
{
bins.Concat(bin);
}
}
);
No comments:
Post a Comment