gpt4 book ai didi

linux - 我能否以编程方式选择我的线程应该在多核 CPU 的哪个内核上运行?

转载 作者:IT王子 更新时间:2023-10-29 00:50:40 26 4
gpt4 key购买 nike

还是由操作系统控制?我听说来自 Google 的新 Go 语言内置了一些功能,可以让程序员实现这种精细化,还是我的理解有误?

最佳答案

对于 linux 操作系统,sched_setaffinity 就是您的答案。它从 linux 内核 2.5.8 开始支持。

名字

sched_setaffinity, sched_getaffinity — 设置和获取进程的 CPU 亲和性掩码

#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);

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().)

关于linux - 我能否以编程方式选择我的线程应该在多核 CPU 的哪个内核上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1854709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com