c++ - 在 Windows 上以编程方式获取硬件线程数的可靠方法

标签 c++ windows multithreading winapi

我正在努力寻找一种可靠的方法来获取 Windows 上的硬件线程数。我在配备双 CPU Intel Xeon E5-2699 v3 @ 2.30GHz 的机器上运行 Windows 7 Professional SP1 64 位,总计 36 个内核和 72 个线程。 我尝试了不同的方法来获取内核数量,但我发现其中只有两个方法在 32 位或 64 位进程中似乎可以准确工作。这是我的结果:

+------------------------------------------------+----------------+----------------+
|                    Methods                     | 32-bit process | 64-bit process |
+------------------------------------------------+----------------+----------------+
| GetSystemInfo->dwNumberOfProcessors            |             32 |             36 |
| GetNativeSystemInfo->dwNumberOfProcessors      |             36 |             36 |
| GetLogicalProcessorInformation                 |             36 |             36 |
| GetProcessAffinityMask.processAffinityMask     |             32 |             32 |
| GetProcessAffinityMask.systemAffinityMask      |             32 |             32 |
| omp_get_num_procs                              |             32 |             36 |
| getenv("NUMBER_OF_PROCESSORS")                 |             36 |             36 |
| GetActiveProcessorCount(ALL_PROCESSOR_GROUPS)  |             64 |             72 |
| GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS) |             64 |             72 |
| boost::thread::hardware_concurrency()          |             32 |             36 |
| Performance counter API                        |             36 |             36 |
| WMI                                            |             72 |             72 |
| HARDWARE\DESCRIPTION\System\CentralProcessor   |             72 |             72 |
+------------------------------------------------+----------------+----------------+

我不解释为什么所有这些函数返回不同的值。只有两种对我来说可靠的方法是使用 WMI(但相当复杂)或简单地在 Windows 注册表中读取以下键:HARDWARE\DESCRIPTION\System\CentralProcessor。

你怎么看? 您确认 WMI 和注册表项方法是唯一可靠的方法吗?

提前致谢

最佳答案

您需要的 API 函数是 GetLogicalProcessorInformationEx。由于您有超过 64 个处理器,因此您的处理器被分组。 GetLogicalProcessorInformation 仅报告线程当前分配的处理器组中的处理器。您需要使用 GetLogicalProcessorInformationEx 来克服该限制。

documentation说:

On systems with more than 64 logical processors, the GetLogicalProcessorInformation function retrieves logical processor information about processors in the processor group to which the calling thread is currently assigned. Use the GetLogicalProcessorInformationEx function to retrieve information about processors in all processor groups on the system.

关于c++ - 在 Windows 上以编程方式获取硬件线程数的可靠方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31209256/

相关文章:

iphone - NSFileManager:继续在后台写入磁盘吗?

java - JFrame 在 thread.sleep() 之前未更新

java - 创建 Java native 接口(interface)的问题

c - 确定 C 中 RegQueryValueEx 所需的缓冲区大小

windows - Windows 上的 .ctags 文件在哪里?

c++ - 让 VS2013、DirectXMath、XAudio2 和 D3D11 一起针对 Windows 7 工作

c++ - 一种类型的十进制表示的长度的近似值

c++ - ‘struct bb’ 类的前向声明

c++ - 垂直打印树

linux - 在内核中使用读写锁死锁