-3

I need set the number of processor's threads for parallel "for-loop". I want work with only 3 threads of 8 (4-cores processor) ..... Now I'm using all 8 threads.

        System.Threading.Tasks.Parallel.For(0, exponentstring.Length, (i, loopState) =>
        {
            int y = my_function (a, exponent[i], modulo);
            string str = y.ToString();
            if (str == numbers[0])
            {
                loopstop = 1;
                loopState.Stop();

            }

        });

Thanks for your ideas.

yoozer8
  • 7,361
  • 7
  • 58
  • 93

2 Answers2

2

Use ParallelOptions class and set MaxDegreeOfParallelism

1

There is an overload that takes an ParallelOptions object:

var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 3;
Parallel.For(0, exponentstring.Length, options, (i, loopState) =>
        {
...
        });
Joey
  • 344,408
  • 85
  • 689
  • 683