0

Why the output for the following program is always 10 threads? I tried to change the max number of threads to the thread pool to 500 but it doesn't change anything.

enter image description here

class Program
{
    private static object _locker = new object();

    static void Main(string[] args)
    {
        
        var data = new Dictionary<int, DateTime>();

        Parallel.For(0, 100000000, new ParallelOptions { MaxDegreeOfParallelism = 500 },
            i =>
            {
                lock (_locker)
                {
                    if (!data.ContainsKey(Thread.CurrentThread.ManagedThreadId))
                    {
                        data.Add(Thread.CurrentThread.ManagedThreadId, DateTime.Now);
                        Console.WriteLine($"{DateTime.Now}, {Thread.CurrentThread.ManagedThreadId}");
                    }
                }
                
            });
    }

}
Igor
  • 281
  • 4
  • 12
  • 2
    It will never use more threads than your computer has CPU cores. There is no reason to use more threads than that (within `Parallel.For`) – Dai Sep 19 '20 at 18:00
  • 5
    Also, your use of `lock` completely defeats the point of using Parallel.For!!! Use `ConcurrentDictionary` instead. – Dai Sep 19 '20 at 18:01
  • 3
    @Dai Operating System threads and CPU cores is not the same thing. You can have more OS threads than CPU cores. Tons of threads if you want. OS manages that with the CPU. The number of cores only allow to execute CoreCount *CPU instructions* at exactly the same time in parallel in the hardware pipes. https://www.guru99.com/cpu-core-multicore-thread.html & https://www.techsiting.com/cores-vs-threads/ –  Sep 19 '20 at 18:06
  • 1
    @OlivierRogier If the CPU has 10 threads (either from 10 cores or likely 5 cores with 2 threads each), at any given time, 10 threads will be active. Windows usually has thousands or even dozens of thousands of threads, doesn't mean they are active though. – Camilo Terevinto Sep 19 '20 at 18:10
  • As the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.paralleloptions.maxdegreeofparallelism?redirectedfrom=MSDN&view=netcore-3.1#System_Threading_Tasks_ParallelOptions_MaxDegreeOfParallelism) say, this setting is designed to **limit** the number of threads used - _By default, For and ForEach will utilize **however many threads the underlying scheduler provides**, so changing MaxDegreeOfParallelism from the default only **limits** how many concurrent tasks will be used._ – stuartd Sep 19 '20 at 18:21
  • 1
    @Dai, but I have only 4 cores on my laptop. So how can 10 threads to be run? – Igor Sep 19 '20 at 18:23
  • 1
    Could you try calling `ThreadPool.SetMinThreads(500, 500);` at the start of the program, to see if it makes any difference? – Theodor Zoulias Sep 19 '20 at 18:34
  • @TheodorZoulias see what I get: https://prnt.sc/uk7rg3 – Igor Sep 19 '20 at 18:39
  • 2
    I voted to reopen because this is a *why* question, while the [duplicate](https://stackoverflow.com/questions/36442750/increase-number-of-running-thread-in-parallel-for) is a *how* question. – Theodor Zoulias Sep 19 '20 at 19:04
  • 2
    @Igor I use “CPU core” as shorthand for “hardware thread” - if you have HyperThreading enabled then each CPU core is capable of running two threads concurrently (though the second hardware thread in each CPU core runs with less performance). So 4 modern CPU cores will give you at least 8 threads. I admit I don’t know where the other two threads are coming from but I suspect the ThreadPool adds 2 more threads if it detects underutilisation which would be caused by your `lock` statement. – Dai Sep 19 '20 at 19:50

0 Answers0