ThreadPool Hard Loop Callbacks - 100% CPU

I have a method in my algorithm that runs a very tight loop on a very large dataset. I originally wrote this single-threaded, which was good, but it took a lot of time. I now want to get acceleration, so now I use ThreadPool to parallelize the work. The problem is that this leads to the fact that the use of my processor reaches 95-100%, which I expect. However, my productivity has increased dramatically, but I think I could do it better if I could reduce all context switches. This also leads to the fact that my other programs are slightly behind as they have to deal with CPU resource flows.

My question is: how do I do this? The only thing I could think of was to limit the number of threads executed at one time, but this could make my algorithm slower, since only a few threads will be able to start at a time. I do not want to add sleep to my threads, as I just need the algorithm to execute as quickly as possible before completion.

EDIT: A few people mentioned using TPL. I think this is a great idea, but unfortunately I forgot to mention that I was stuck using .NET 3.5, since the parent application has not yet released a version using .NET 4.

+5
source share
2 answers

. , . " , " " , ". ; , , , . , .

, , :

  • ThreadPool - . ThreadPool " , ". , , , ThreadPool ( , ), . ThreadPool, , , ( ) ( ) , . TPL, , ( ).

  • , "" . , , , , , . , , " " ( CPU " ", HyperThreading, ), ( " " ), . , " "; , , . , ThreadPool , , , .

  • , . ( " ( ) -" ) . ( ), " - " ( ), , , , , , , , .

+6

foreach IEnumerable, PLINQ, . WithDegreeOfParallelism, , . "", , . , , , . PLINQ .

, :

var arrayOfStuff = new[] { ... };
for (var i = 0; i < arrayOfStuff.Length; ++i)
  DoSomething(arrayOfStuff[i]);

, , PLINQ, ​​, :

var cores = Math.Max(1, Environment.ProcessorCount - 1);
arrayOfStuff.AsParallel().WithDegreeOfParallelism(cores).ForAll(DoSomething);

, , :

IEnumerable<Stuff> GetStuff() {
  for ( ... very complex looping ... ) {
    ...
    yield return stuff;
  }
}
+2

All Articles