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

前端 未结 5 1168
我在风中等你
我在风中等你 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:41

    From output of man sched_getcpu:

    NAME
           sched_getcpu - determine CPU on which the calling thread is running
    
    SYNOPSIS
           #define _GNU_SOURCE
           #include 
    
           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.

提交回复
热议问题