我正在尝试使用Parallel.for循环来加速我的过程。我无法完成这项工作,因为我使用的索引超出了界限。在研究这个网站之后,我想我知道我在做什么doing wrong,并且我认为我也以临时变量的形式找到了解决方案,这些临时变量在进入代码中的动作之前存储了循环变量。然而,这在我的例子中不起作用。我发现有人提供给System.Collections.Concurrent的链接,它可以为像这样的情况提供安全线程,但我不知道如何使用该集合。我如何去做这件事?在Parallel.for循环中超出范围
我试图创建一个复制粘贴代码为你们跑,但我做错事的时候有这可能表明我的经验不足:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
namespace empty
{
class Program
{
double[, , ,] info = new double[100, 100, 10, 5];
void calculate()
{
int row;
int layer, tex_class;
double result;
try
{
for (row = 0; row < 100; row++)
{
Parallel.For(0, 99, col =>
{
int tempcol = col;
for (layer = 0; layer < 10; layer++)
{
int templayer = layer;
for (tex_class = 0; tex_class < 5; tex_class++)
{
int tempclass = tex_class;
result = info[row, tempcol, templayer, tempclass];
}
//other code
}
});
}
}
catch { }
}
static void Main(string[] args)
{
calculate();
}
}
}
哦,我的4维阵列。 –
代码应该做什么?我根本不理解它。也许如果你发布一些预期的输出...... –