我可以以编程方式选择线程应在多核CPU的哪个核上运行吗? [英] Can I programmatically pick and choose which core of a multi-core CPU my thread should run on?
问题描述
还是由操作系统控制?我听说Google推出了新的Go语言,它具有供程序员使用的内置功能,还是我理解错了?
Or is it controlled by the operating system? I hear the new Go language from Google has built-in features for a programmer to go that granular, or have I understood it wrong?
推荐答案
对于linux操作系统, sched_setaffinity 是您的答案.从Linux内核2.5.8开始受支持.
For linux os, sched_setaffinity is your answer. It's supported since linux kernel 2.5.8.
名称
sched_setaffinity,sched_getaffinity —设置并获取进程的CPU亲和力掩码
sched_setaffinity, sched_getaffinity — set and get a process's CPU affinity mask
#define _GNU_SOURCE
#include <sched.h>
int sched_setaffinity( pid_t pid,
size_t cpusetsize,
cpu_set_t *mask);
int sched_getaffinity( pid_t pid,
size_t cpusetsize,
cpu_set_t *mask);
亲和力遮罩实际上是一个 每个线程的属性可以是 为每个独立调整 线程组中的多个线程.价值 从对gettid(2)的调用返回的值可以 在参数pid中传递. 将pid指定为0将设置 调用线程的属性,以及 传递调用返回的值 getpid(2)将设置属性 用于线程的主线程 团体. (如果您使用的是POSIX 线程API,然后使用 pthread_setaffinity_np(3)代替 sched_setaffinity().)
The affinity mask is actually a per-thread attribute that can be adjusted independently for each of the threads in a thread group. The value returned from a call to gettid(2) can be passed in the argument pid. Specifying pid as 0 will set the attribute for the calling thread, and passing the value returned from a call to getpid(2) will set the attribute for the main thread of the thread group. (If you are using the POSIX threads API, then use pthread_setaffinity_np(3) instead of sched_setaffinity().)
这篇关于我可以以编程方式选择线程应在多核CPU的哪个核上运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!