线程亲和力还限制内存分配?

时间:2014-06-16 02:54:24

标签: c multithreading memory memory-management

我目前正在开发一个带有两个CPU的NUMA系统。两个CPU的内存访问时间不同。

我使用PTHREAD_SETAFFINITY_NP将线程分配给特定的CPU:

int stick_this_thread_to_core(int core_id) {
  int num_cores = sysconf(_SC_NPROCESSORS_ONLN);//number of processors
  if (core_id < 0 || core_id >= num_cores)
     return EINVAL;
  cpu_set_t mask;
  CPU_ZERO(&mask);
  CPU_SET(core_id, &mask);
  pthread_t current_thread = pthread_self();    
  return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &mask);
}

我的问题是,它是否也限制了线程的内存分配?例如,如果在CPU-1中运行的线程中有malloc,那么内存是否只分配在CPU-1的内存中?

1 个答案:

答案 0 :(得分:2)

我不会指望它。在Windows中,您必须使用特殊函数来对特定numa节点上的内存进行allcate。 Linux非常相似。在linux中寻找一个带有numa节点的内存映射函数。

编辑: numa_malloc

请参阅此链接

NUMA aware cache aligned memory allocation

相关问题