c - 线程亲和性也限制内存分配?

标签 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的内存中?

最佳答案

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

编辑: numa_malloc

查看此链接

NUMA aware cache aligned memory allocation

关于c - 线程亲和性也限制内存分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24236087/

相关文章:

c - IPv6 绑定(bind)失败

iphone - 从 NSData 读取 NSNumber

c# - 线程中的 powershell/runspace

java - Java中的线程何时从内存中删除?

C : uninitialized char pointer and print out the address

安卓内存管理

c - modbus 十六进制地址转换

c++ - cimg display() 什么也没有,但调试消息是正确的

multithreading - 是否可以在 riscv 中的两个锁上构建原子 "release-and-acquire"操作?

c# - 非阻塞锁定