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.
source
share