如何配置并行的最大线程数
本文关键字:线程 并行 何配置 配置 | 更新日期: 2023-09-27 18:08:24
这是微软为并行提供的示例,我想知道如何为该代码配置最大线程数。
// A basic matrix multiplication.
// Parallelize the outer loop to partition the source array by rows.
System.Threading.Tasks.Parallel.For(0, matARows, i =>
{
for (int j = 0; j < matBCols; j++)
{
// Use a temporary to improve parallel performance.
double temp = 0;
for (int k = 0; k < matACols; k++)
{
temp += matA[i, k] * matB[k, j];
}
result[i, j] = temp;
}
}); // Parallel.For
需要用MaxDegreeOfParallelism
:
ParallelOptions
值例如:Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, count =>
{
Console.WriteLine(count);
});
我建议你看一下ParallelOption。maxdegreesofparallelism并将其传递给For
方法
使用maxdegreeofparallelism属性运行循环
Parallel.For(0, 1000, new ParallelOptions { MaxDegreeOfParallelism = 2 }, ...);