如何找到运行线程的处理器编号?

Pat*_*ski 15 c linux windows-xp memory-management pthreads

我有一个内存堆管理器,它根据系统上的处理器数量将堆分成不同的段.内存只能分配给当前运行的线程处理器的分区.这将有助于不同的处理器继续运行,即使两个不同的处理器想要同时分配内存,至少我相信.

我找到了GetCurrentProcessorNumber()Windows 的功能,但这仅适用于Windows Vista及更高版本.有没有一种适用于Windows XP的方法?

另外,这可以通过POSIX系统上的pthread来完成吗?

Ili*_* K. 9

从输出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)
Run Code Online (Sandbox Code Playgroud)

不幸的是,这是特定于Linux的.我怀疑有一种可移植的方式来做到这一点.


小智 8

对于XP,一个快速的谷歌透露:https: //www.cs.tcd.ie/Jeremy.Jones/GetCurrentProcessorNumberXP.htm这有帮助吗?