none
How to see how many cores are running for multi threaded application RRS feed

  • Question

  • I have 8 core pc. I need some UI which show me how many core are running for my plinq query in Winform  application where i have mentioned no of thread to run.

    I need to see is my 8 core are utilize or few are running?

    Please give me some cool hint by which i can visualize how many core are running. please guide me Thanks

    Monday, February 3, 2020 7:55 PM

Answers

  • Have you looked a PerformanceCounter?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Sudip_inn Wednesday, February 5, 2020 7:43 AM
    Monday, February 3, 2020 9:10 PM
    Moderator
  • Hi Sudip_inn,

    Thank you for posting here.

    First, I want to say that PLINQ uses all of the processors on the host computer by default.

    You can instruct PLINQ to use no more than a specified number of processors by using the WithDegreeOfParallelism method.

    Introduction to PLINQ

    Second, the CPU is always working for all processes, and I'm not sure if there is a way to confirm which ones are working for PLINQ.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, February 4, 2020 2:14 AM
  • Ok, I wrote a small program to count how many threads are launched by Parallel.Foreach:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace CountCores
    {
        class Program
        {
            private static int count = 0;
            private const int WaitTime = 200;
    
            static void Main(string[] args)
            {
                Thread t = new Thread(RunLoop);
                t.IsBackground = true;
                t.Start();
                Thread.Sleep(WaitTime);
                t.Interrupt();
                Console.WriteLine($"Counted {count} threads");
                Console.ReadLine();
            }
    
            private static void RunLoop()
            {
                int[] array = new int[128];
                Parallel.ForEach(array, a => Tally(a));
            }
    
            private static void Tally(int a)
            {
                try
                {
                    Interlocked.Increment(ref count);
                    Thread.Sleep(WaitTime * 2);
                }
                catch (ThreadInterruptedException)
                {
                    // Expected. Just exit.
                }
            }
        }
    }
    

    In a machine with 8 cores, it returns "9".

    And in a machine with 24 cores (actually, 12 cores with hyperthreading) it returns "25".

    The inference is that Parallel.Foreach is scheduling one more thread than the number of cores that you actually have. I imagine that they do this to keep all your CPUs busy during the time that one thread finishes and another one is launched, but I have not seen this documented.

    • Marked as answer by Sudip_inn Wednesday, February 5, 2020 7:43 AM
    Tuesday, February 4, 2020 10:31 AM
    Moderator

All replies

  • Have you looked a PerformanceCounter?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Sudip_inn Wednesday, February 5, 2020 7:43 AM
    Monday, February 3, 2020 9:10 PM
    Moderator
  • Hi Sudip_inn,

    Thank you for posting here.

    First, I want to say that PLINQ uses all of the processors on the host computer by default.

    You can instruct PLINQ to use no more than a specified number of processors by using the WithDegreeOfParallelism method.

    Introduction to PLINQ

    Second, the CPU is always working for all processes, and I'm not sure if there is a way to confirm which ones are working for PLINQ.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, February 4, 2020 2:14 AM
  • Ok, I wrote a small program to count how many threads are launched by Parallel.Foreach:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace CountCores
    {
        class Program
        {
            private static int count = 0;
            private const int WaitTime = 200;
    
            static void Main(string[] args)
            {
                Thread t = new Thread(RunLoop);
                t.IsBackground = true;
                t.Start();
                Thread.Sleep(WaitTime);
                t.Interrupt();
                Console.WriteLine($"Counted {count} threads");
                Console.ReadLine();
            }
    
            private static void RunLoop()
            {
                int[] array = new int[128];
                Parallel.ForEach(array, a => Tally(a));
            }
    
            private static void Tally(int a)
            {
                try
                {
                    Interlocked.Increment(ref count);
                    Thread.Sleep(WaitTime * 2);
                }
                catch (ThreadInterruptedException)
                {
                    // Expected. Just exit.
                }
            }
        }
    }
    

    In a machine with 8 cores, it returns "9".

    And in a machine with 24 cores (actually, 12 cores with hyperthreading) it returns "25".

    The inference is that Parallel.Foreach is scheduling one more thread than the number of cores that you actually have. I imagine that they do this to keep all your CPUs busy during the time that one thread finishes and another one is launched, but I have not seen this documented.

    • Marked as answer by Sudip_inn Wednesday, February 5, 2020 7:43 AM
    Tuesday, February 4, 2020 10:31 AM
    Moderator
  • How to use perfmon to see how many core are running....any idea?
    Wednesday, February 5, 2020 7:43 AM