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

前端 未结 5 1160
我在风中等你
我在风中等你 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.

    0 讨论(0)
  • 2020-12-15 22:37

    For XP, a quick google as revealed this: https://www.cs.tcd.ie/Jeremy.Jones/GetCurrentProcessorNumberXP.htm Does this help?

    0 讨论(0)
  • 2020-12-15 22:41

    From output of man sched_getcpu:

    NAME
           sched_getcpu - determine CPU on which the calling thread is running
    
    SYNOPSIS
           #define _GNU_SOURCE
           #include <utmpx.h>
    
           int sched_getcpu(void);
    
    DESCRIPTION
       sched_getcpu() returns the number of the CPU
       on which the calling thread is currently executing.
    
    RETURN VALUE
       On success, sched_getcpu() returns a non-negative CPU number.
       On error, -1 is returned and errno is set to indicate the error.
    
    SEE ALSO
       getcpu(2)
    

    Unfortunately, this is Linux specific. I doubt there is a portable way to do this.

    0 讨论(0)
  • 2020-12-15 22:46

    This design smells bad to me. You seem to be making the assumption that a thread will stay associated with a specific CPU. That is not guaranteed. Yes, a thread may normally stay on a single CPU, but it doesn't have to, and eventually your program will have a thread that switches CPU's. It may not happen often, but eventually it will. If your design doesn't take this into account, then you will mostly likely eventually hit some sort of hard to trace bug.

    Let me ask this question, what happens if memory is allocated on one CPU and freed on another? How will your heap handle that?

    0 讨论(0)
  • 2020-12-15 22:48

    If all you want to do is avoid contention, you don't need to know the current CPU. You could just randomly pick a heap. Or you could have a heap per thread. Although you may get more or less contention that way, you would avoid the overhead of polling the current CPU, which may or may not be significant. Also check out Intel Thread Building Block's scalable_allocator, which may have already solved that problem better than you will.

    0 讨论(0)
提交回复
热议问题