How to configure a maximum number of threads in a Parallel.For

后端 未结 3 790
执念已碎
执念已碎 2021-01-01 12:00

This is the example microsoft presents for the parallel for, and I\'d like to know how configure a maximum number of threads for this code.

     // A basic m         


        
相关标签:
3条回答
  • 2021-01-01 12:20

    Use MaxDegreeOfParalelism property for running the loop

    Parallel.For(0, 1000, new ParallelOptions { MaxDegreeOfParallelism = 2 }, ...);
    
    0 讨论(0)
  • 2021-01-01 12:21

    You need to specify a ParallelOptions value with a MaxDegreeOfParallelism:

    For example:

    Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, count =>
    {
        Console.WriteLine(count);
    });
    
    0 讨论(0)
  • 2021-01-01 12:33

    I'd suggest you take a look at the ParallelOption.MaxDegreesofParellelism and pass it into the For method

    0 讨论(0)
提交回复
热议问题