- Home /
How can I get the number of physical cores in the CPU (not logical cores)
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. Is there other way to get that information ?
Answer by Harry_Drew · Jun 09, 2021 at 09:44 AM
Hi, 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 :).
Hi, 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
Your answer
Follow this Question
Related Questions
Call long running AI function without tying up game loop? 1 Answer
Threading. What is it? 2 Answers
Having issues when multithreading and deserializing JSON with JSONUtility 1 Answer
Using the GPU instead of multiple threads 1 Answer
3D voxel Planet (dual contouring) Job System or Task Parallel Library? 1 Answer