How to get number of cores in Win32?
Asked Answered
C

5

11

I'm writing a program in C on windows that needs to run as many threads as available cores. But I dont know how to get the number of cores. Any ideas?

Counterproposal answered 11/4, 2010 at 23:57 Comment(4)
As often is the case, "How to get number of cores in WINDOWS/using Windows API" is a better title than "How to get number of cores in C". It depends on the available APIs (and platform), not the programming language.Huber
exact duplicate : uni processor or multi processorCerebro
For portable info, also see: Programmatically find the number of cores on a machine.Crawly
@adcdefg That's for unix not win32.Huffy
M
10

You can call the GetSystemInfo WinAPI function; it returns a SYSTEM_INFO struct, which has the number of processors (which is the number of cores on a system with multiple core CPUs).

Monacid answered 11/4, 2010 at 23:59 Comment(3)
What about systems with multiple multi-core CPUs? I'm pretty sure Windows drills down to that detail somewhere, although I don't know if it matters here.Poet
dwNumberOfProcessors is the logical processor count, not cores.Together
This is limited to 32 on 32-bit systems, and 64 on 64-bit systems, because it only returns the number of processors in the current processor group. On systems with more cores than this it will not return the correct value. It also does not return the correct value for 32-bit processes running under WOW64.Hyperbaton
D
6

You can read NUMBER_OF_PROCESSORS environment variable.

Degas answered 12/4, 2010 at 0:0 Comment(2)
It's generally better to call an API function rather than rely on a secondary environment variable (which could be changed by something else before running your program).Sporangium
Depending on the purpose of the question this might be the better answer. If you want to find out how the number of cores to decide how many threads to start then this environment variable should be considered because a parent on a quad core could start to processes and pass both of them a value of 2 instead of 4. I think in 90% of all cases this is exactly what is wanted (well unless the developers realise that only full system OS utilisation aware thread pools controlled by the OS should be used, maybe in 5 to 10 years. Programmers are retarded).Greylag
U
4

As @Changming-Sun mentioned in a comment above, GetSysInfo returns the number of logical processors, which is not always the same as the number of processor cores. On machines that support hyperthreading (including most modern Intel CPUs) more than one thread can run on the same core (technically, more than one thread will have its thread context loaded on the same core). Getting the number of processor cores requires a call to GetLogicalProcessorInformation and a little bit of coding work. Basically, you get back a list of SYSTEM_LOGICAL_PROCESSOR_INFORMATION entries, and you have to count the number of entries with RelationProcessorCore set. A good example of how to code this in the GetLogicalProcessorInformation documentation provided by Microsoft: https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformation

Undressed answered 1/2, 2020 at 23:20 Comment(0)
H
2

Type "cmd" on windows startup and open "cmd.exe". Now type in the following command:

WMIC CPU Get /Format:List

You will find the entries like - "NumberOfCores" and "NumberOfLogicalProcessors". Typically the logical-processors are achieved by threading. Therefore the relation would typically go like;

NumberOfLogicalProcessors = NumberOfCores * Number-of-Threads-per-Core.

Since each core serves a processing-unit, therefore with threading, logical-processing-unit is realized in real space.

More info here.

Honest answered 19/1, 2017 at 6:9 Comment(0)
O
1

Even though the question deals with .NET and yours with C, the basic responses should help:

Detecting the number of processors

Orfurd answered 12/4, 2010 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.