本文介绍了如何设置特定pthread的CPU亲和力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指定特定pthread的cpu亲和力.到目前为止,我发现的所有引用都涉及设置进程(pid_t)而不是线程(pthread_t)的cpu亲和力.我尝试了一些传递pthread_t的实验,但按预期它们失败了.我是否在尝试做一些不可能的事情?如果没有,您可以发送指针吗?谢谢你一百万.

I'd like to specify the cpu-affinity of a particular pthread. All the references I've found so far deal with setting the cpu-affinity of a process (pid_t) not a thread (pthread_t). I tried some experiments passing pthread_t's around and as expected they fail. Am I trying to do something impossible? If not, can you send a pointer please? Thanks a million.

推荐答案

这是我用来简化生活的包装器.它的作用是使调用线程停留"在ID为core_id的内核上:

This is a wrapper I've made to make my life easier. Its effect is that the calling thread gets "stuck" to the core with 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);
}

这篇关于如何设置特定pthread的CPU亲和力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:36