gpt4 book ai didi

c - 如何设置特定 pthread 的 CPU 关联性?

转载 作者:行者123 更新时间:2023-12-03 05:49:30 28 4
gpt4 key购买 nike

我想指定特定 pthread 的 CPU 亲和性。到目前为止我找到的所有引用资料都涉及设置进程(pid_t)而不是线程(pthread_t)的CPU亲和性。我尝试了一些传递 pthread_t 的实验,但正如预期的那样,它们失败了。我正在尝试做一些不可能的事情吗?如果没有的话,可以发一份指点吗?谢谢一百万。

最佳答案

这是我为了让我的生活更轻松而制作的包装。其效果是调用线程“卡”在 id core_id:

的核心上
// core_id = 0, 1, ... n-1, where n is the system's number of cores

int stick_this_thread_to_core(int core_id) {
int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
if (core_id < 0 || core_id >= num_cores)
return EINVAL;

cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);

pthread_t current_thread = pthread_self();
return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
}

关于c - 如何设置特定 pthread 的 CPU 关联性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1407786/

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