0

I want to convert a large array of objects in parallel using PLINQ.

var outputs = inputs .AsParallel() .AsOrdered() .WithDegreeOfParallelism(8) // make this number dynamic .Select(input => MyConverter(input)) .ToArray(); 

Is there a way to change the number of threads used during execution (custom control)? E.g. by using a callback function like this:

int CurrentThreadLimit() => DateTime.Now.Second / 4 + 1; // callback function (example) var outputs = inputs .AsParallel() .AsOrdered() .WithDynamicDegreeOfParallelism(() => CurrentThreadLimit()) // How to build this? .Select(input => MyConverter(input)) .ToArray(); 

Is something like this possible?

8
  • Related: Is it possible to change parallelOptions.MaxDegreeOfParallelism during execution of a Parallel.ForEach? Commented Jun 16, 2023 at 17:47
  • Already answered: stackoverflow.com/a/25290512/8978576 Commented Jun 16, 2023 at 18:50
  • 1
    @Serhii I don't see a solution there for variable degree of parallelism during execution. Commented Jun 16, 2023 at 21:17
  • Can't you make it a function that returns a degree of parallelism to a variable and then use this variable during a query? int degree = GetDegreeOfParallelism(); ...AsParallel().WithDegreeOfParallelism(degree); Commented Jun 18, 2023 at 1:22
  • 1
    @Serhii the OP wants to change the degree of parallelism during a single execution of the query. For example start with parallelism 4, then change it to 2, and then change it back to 4 before the execution ends. They don't want a constant degree of parallelism from start to finish. Commented Jun 18, 2023 at 3:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.