windows 如何获得Win32的核心数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2619198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:15:32  来源:igfitidea点击:

How to get number of cores in Win32?

cwindowsmultithreadingwinapicpu-cores

提问by Alan

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?

我正在 Windows 上用 C 编写一个程序,该程序需要运行与可用内核一样多的线程。但我不知道如何获得核心数。有任何想法吗?

回答by James McNellis

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

可以调用GetSystemInfoWinAPI函数;它返回一个SYSTEM_INFO结构体,其中包含处理器的数量(这是具有多核 CPU 的系统上的核数)。

回答by Josip Medved

You can read NUMBER_OF_PROCESSORS environment variable.

您可以读取 NUMBER_OF_PROCESSORS 环境变量。

回答by DSimkin

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://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformation

正如@Changming-Sun 在上面的评论中提到的,GetSysInfo 返回逻辑处理器的数量,它并不总是与处理器内核的数量相同。在支持超线程(包括大多数现代 Intel CPU)的机器上,可以在同一个内核上运行多个线程(从技术上讲,多个线程将在同一个内核上加载其线程上下文)。获得处理器内核的数量需要调用 GetLogicalProcessorInformation 和一些编码工作。基本上,您会得到一个 SYSTEM_LOGICAL_PROCESSOR_INFORMATION 条目列表,并且您必须计算具有 RelationProcessorCore 设置的条目数。关于如何在 Microsoft 提供的 GetLogicalProcessorInformation 文档中进行编码的一个很好的例子: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformation

回答by James Black

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

即使这个问题涉及 .NET 而你的问题涉及 C,基本的回答应该会有所帮助:

Detecting the number of processors

检测处理器数量

回答by parasrish

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

在 Windows 启动时输入“cmd”并打开“cmd.exe”。现在输入以下命令:

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;

您会找到诸如“NumberOfCores”和“NumberOfLogicalProcessors”之类的条目。通常逻辑处理器是通过线程实现的。因此,这种关系通常会像这样;

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

NumberOfLogicalProcessors = NumberOfCores * 每核线程数。

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

由于每个内核服务于一个处理单元,因此通过线程,逻辑处理单元是在现实空间中实现的。

More info here.

更多信息在这里