C/C++/Assembly Programatically detect if hyper-threading is active on Windows, Mac and Linux [duplicate]

戏子无情 提交于 2019-11-30 20:31:13

Linux:

Number of physical CPUs:

grep -i "physical id" /proc/cpuinfo | sort -u | wc -l

Number of logical CPUs:

grep -i "processor" /proc/cpuinfo | sort -u | wc -l

On Windows 2003 Server and Windows XP SP3 and later, you can determine this information using the GetLogicalProcessorInformation system call.

The CPUID instruction (when you pass function 1H in EAX) returns they hyper threading feature flag in bit 28 of the EDX register. I think that multi-core processors report that they are hyperthreading enabled even though each individual core can run only one thread.

It also returns the number of logical processors per physical processor in bits 23-16 of EBX. I think that you'd have to query each processor individually in order to hit all of the processors on your system.

On OS X:

#include <sys/sysctl.h>

int physicalCores;
sysctlbyname("hw.physicalcpu", &physicalCores, sizeof(physicalCores), NULL, 0);

See the header or manpage for more information. (Note that you can get the number of logical cpus in the same way, using the "hw.logicalcpu" string)

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