I am implementing multithreading and I need the real number of physical cores to avoid creating more threads than CPU can handle. There is SystemInfo.processorCount but returns the number of threads (logical cores) instead of physical ones.
1 Answer
1Hi, this is how I would do it in C#:
using System;
class Sample
{
public static void Main()
{
Console.WriteLine(
"The number of processors on this computer is {0}.",
Environment.ProcessorCount
);
}
}
To adapt this for unity this should work:
using System;
void Start()
{
Debug.log(Environment.ProcessorCount);
}
Please note that this is quite inefficient
I am still getting 6 cores, maybe is because I use an FX6300 that doesn't have 6 real cores and tricks the Inspector in thinking that there are 6. Problem is if I start more than 3 threads the performance decreases until single thread performance is higher. FX6300 should be advertised as 3 cores and 6 threads not 6 cores 6 thread, I will test this on another CPU to confirm. Thanks anyway :).
– CoreDLLHi, sorry to hear that your still having trouble here is a link to a stack overflow thread that goes into the topic in more detail although the code is written for straight C# not C# with unity. : https://stackoverflow.com/questions/1542213/how-to-find-the-number-of-cpu-cores-via-net-c
– Harry_Drew