Alex Rivera | Logout

Setting the cores to use in Parallelism

Asked 2010-06-02T10:11:29.587
9

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

Edit
Report

1 Answer

9

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) => {
      //..
    });
  }
}
answered 2010-06-02T11:55:05.003

Your Answer