How to get number of cores in Win32?

元气小坏坏 提交于 2019-11-27 19:30:08

问题


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?


回答1:


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).




回答2:


You can read NUMBER_OF_PROCESSORS environment variable.




回答3:


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

Detecting the number of processors




回答4:


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.



来源:https://stackoverflow.com/questions/2619198/how-to-get-number-of-cores-in-win32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!