0

In C# I have function that do something with many string lines that I want separate to threads. For example 1000 string lines for one thread. Therefore how can separate thread to multiple threads for efficiently using CPU?

abberdeen
  • 323
  • 7
  • 32
  • 2
    Does this answer your question? [Maximum number of threads in a .NET app?](https://stackoverflow.com/questions/145312/maximum-number-of-threads-in-a-net-app) – Trevor Nov 04 '19 at 19:58

1 Answers1

2

Parallel.Foreach allows you to split the work from an IEnumerable into multiple parallel parts and you can control the maximum number of threads that can process that enumeration.

Parallel.ForEach(
    stringLines,
    new ParallelOptions { MaxDegreeOfParallelism = 42 },
    line=> { DoWork(line); }
);
Claudiu Guiman
  • 837
  • 5
  • 18
  • [System.Environment.ProcessorCount])https://learn.microsoft.com/en-us/dotnet/api/system.environment.processorcount) would give you the number of processors available. There's no maximum number of threads (tehnically not true, but it's a very large number which you shouldn't bother with). Depending on the work you're doing you can decide how many threads you need; As a good practice you shouldn't need to use more threads than the number of CPU cores because they will never all run in parallel. – Claudiu Guiman Nov 04 '19 at 20:00
  • Thanks for answer, but i mean how can define concrete number for example 42? without knowing max? – abberdeen Nov 04 '19 at 20:02
  • I just picked a [random number](https://en.wikipedia.org/wiki/42_(number)). you know your workload better and you should be able to come up with a better number – Claudiu Guiman Nov 04 '19 at 20:03
  • Ok, counterquestion, Parallel.ForEach will automatically "separate function to threads" if so, will be Parallel.ForEach use CPU cores in max possible for application thread? – abberdeen Nov 04 '19 at 20:12
  • That's implementation detail. You can find more information [here](https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/potential-pitfalls-in-data-and-task-parallelism). If you really want to control the number of threads, just created them [yourself](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread), but if you need them there's a possibility you're doing something wrong :) – Claudiu Guiman Nov 04 '19 at 20:17
  • Thanks a lot. I think Parallel.ForEach is that i need... now testing. – abberdeen Nov 04 '19 at 20:24
  • 2
    `Parallel.ForEach` isn't the ultimate solution. It has its own pros and cons. And I would say quite a lot of drawbacks. There are numerous ways to achieve what you need. Just pick the right now for your case – OlegI Nov 04 '19 at 20:31
  • @OlegI Which best way you can advise? – abberdeen Nov 04 '19 at 20:37
  • 2
    `Parallel.ForEach` is very CPU heavy. If it will be running on the client-side, it's fine. But if you are willing to put it on the server, it can simply put the machine down very quickly. Look at ` PLINQ ` it consumes less CPU – OlegI Nov 04 '19 at 20:41