Search code examples
c#.netmultithreadingcpu-cores

Is there a way to reliably detect the total number of CPU cores?


I need a reliable way to detect how many CPU cores are on a computer. I am creating a numerically intense simulation C# application and want to create the maximum number of running threads as cores. I have tried many of the methods suggested around the internet like Environment.ProcessorCount, using WMI, this code: http://blogs.adamsoftware.net/Engine/DeterminingthenumberofphysicalCPUsonWindows.aspx None of them seem to think a AMD X2 has two cores. Any ideas?

Edit: it appears that Environment.ProcessorCount is returning the correct number. It's on a intel CPU with hyperthreading that is returning the wrong number. A signle core with hyperthreading is returning 2, when it should only be 1.


Solution

  • See Detecting the number of processors

    Alternatively, use the GetLogicalProcessorInformation() Win32 API: http://msdn.microsoft.com/en-us/library/ms683194(VS.85).aspx