开发者

Setting the cores to use in Parallelism

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜