开发者

Setting the cores to use in Parallelism

开发者 https://www.devze.com 2023-01-02 12:41 出处:网络
I have a feeling the answer to this is no, but using .Net 4.0\'s开发者_开发技巧 Parallelism, can you set the amount of cores on which to run i.e. if your running a Quad Core, can you set your Applicat

I have a feeling the answer to this is no, but using .Net 4.0's开发者_开发技巧 Parallelism, can you set the amount of cores on which to run i.e. if your running a Quad Core, can you set your Application to only use 2 of them?

Thanks


Yes, it is a built-in capability of Parallel.For(). Use one of the overloads that accepts a ParallelOptions object, set its MaxDegreeOfParallelism property. For example:

using System;
using System.Threading.Tasks;

class Program {
  static void Main(string[] args) {
    var options = new ParallelOptions();
    options.MaxDegreeOfParallelism = 2;
    Parallel.For(0, 100, options, (ix) => {
      //..
    });
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消