How can you find the processor number a thread is running on?

前端 未结 5 1170
我在风中等你
我在风中等你 2020-12-15 22:15

I have a memory heap manager which partitions the heap into different segments based on the number of processors on the system. Memory can only be allocated on the partitio

5条回答
  •  半阙折子戏
    2020-12-15 22:24

    In addition to Antony Vennard's answer and the code on the cited site, here is code that will work for Visual C++ x64 as well (no inline assembler):

    DWORD GetCurrentProcessorNumberXP() {
       int CPUInfo[4];   
       __cpuid(CPUInfo, 1);
       // CPUInfo[1] is EBX, bits 24-31 are APIC ID
       if ((CPUInfo[3] & (1 << 9)) == 0) return -1;  // no APIC on chip
       return (unsigned)CPUInfo[1] >> 24;
    }
    

    A short look at the implementation of GetCurrentProcessorNumber() on Win7 x64 shows that they use a different mechanism to get the processor number, but in my (few) tests the results were the same for my home-brewn and the official function.

提交回复
热议问题