Reputation: 33669
I made a function (see below) which detects if a CPU core has Hyper-threading. When I disable Hyper-threading in the BIOS CPUID still reports that the core has Hyper-threading. How can I do this properly to find out if Hyper-threading is enabled?
// input: eax = functionnumber, ecx = 0
// output: eax = output[0], ebx = output[1], ecx = output[2], edx = output[3]
//static inline void cpuid (int output[4], int functionnumber)
bool hasHyperThreading() {
int abcd[4];
cpuid(abcd,1);
return (1<<28) & abcd[3];
}
Upvotes: 1
Views: 1375
Reputation: 5203
The answer to the first part of your question is "for historical reasons", i.e. that bit is still on even for a processor that is only multi-core; the details are over here.
To figure out if you have HT enabled you need enumerate the active logical processors; it's OS-dependent how to do that. Then for every active logical processor get its x2APIC id (you need to run on it to get that, so you need to able to set the thread-to-processor affinity) and test the last bit. If no HT is enabled, then this bit will not be set for any logical processor. Good source code for doing this is provided by Intel. I see Z boson has provided an alternative, OpenMP-based method, which however needs a little teak to actually answer the question as asked (see my comment there.)
Also, as far as I know AMD currently has no hyperthreading for any of their processors.
Upvotes: 2