linux - CPU 亲和性掩码(将线程放在不同的 CPU 上)

标签 linux multithreading pthreads parallel-processing

我有 4 个线程,我试图将线程 1 设置为在 CPU 1 上运行,线程 2 在 CPU 2 上运行,等等。 然而,当我运行下面的代码时,关联掩码返回正确的值,但是当我在线程上执行 sched_getcpu() 时,它们都返回它们正在 CPU 4 上运行。

有人知道我的问题是什么吗?

提前致谢!

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>

void *pthread_Message(char *message)
{
    printf("%s is running on CPU %d\n", message, sched_getcpu());
}

int main()
{
    pthread_t thread1, thread2, thread3, thread4;
    pthread_t threadArray[4];
    cpu_set_t cpu1, cpu2, cpu3, cpu4;
    char *thread1Msg = "Thread 1";
    char *thread2Msg = "Thread 2";
    char *thread3Msg = "Thread 3";
    char *thread4Msg = "Thread 4";
    int thread1Create, thread2Create, thread3Create, thread4Create, i, temp;

    CPU_ZERO(&cpu1);
    CPU_SET(1, &cpu1);
    temp = pthread_setaffinity_np(thread1, sizeof(cpu_set_t), &cpu1);
    printf("Set returned by pthread_getaffinity_np() contained:\n");
    for (i = 0; i < CPU_SETSIZE; i++)
        if (CPU_ISSET(i, &cpu1))
            printf("CPU1: CPU %d\n", i);

    CPU_ZERO(&cpu2);
    CPU_SET(2, &cpu2);
    temp = pthread_setaffinity_np(thread2, sizeof(cpu_set_t), &cpu2);
    for (i = 0; i < CPU_SETSIZE; i++)
        if (CPU_ISSET(i, &cpu2))
            printf("CPU2: CPU %d\n", i);

    CPU_ZERO(&cpu3);
    CPU_SET(3, &cpu3);
    temp = pthread_setaffinity_np(thread3, sizeof(cpu_set_t), &cpu3);
    for (i = 0; i < CPU_SETSIZE; i++)
        if (CPU_ISSET(i, &cpu3))
            printf("CPU3: CPU %d\n", i);

    CPU_ZERO(&cpu4);
    CPU_SET(4, &cpu4);
    temp = pthread_setaffinity_np(thread4, sizeof(cpu_set_t), &cpu4);
    for (i = 0; i < CPU_SETSIZE; i++)
        if (CPU_ISSET(i, &cpu4))
            printf("CPU4: CPU %d\n", i);

    thread1Create = pthread_create(&thread1, NULL, (void *)pthread_Message, thread1Msg);
    thread2Create = pthread_create(&thread2, NULL, (void *)pthread_Message, thread2Msg);
    thread3Create = pthread_create(&thread3, NULL, (void *)pthread_Message, thread3Msg);
    thread4Create = pthread_create(&thread4, NULL, (void *)pthread_Message, thread4Msg);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);

    return 0;
}

最佳答案

您正在尝试设置您未初始化的线程的亲和力。

编辑:好的,让我给你一些更多的信息:

不要混合线程句柄(存储在 pthread_t 变量中的东西)和它们代表的内容(在某处运行的执行线程)。您试图做的是在线程启动之前使用需要线程对象的 API 设置线程的属性。碰巧 pthread_create 创建对象并同时开始执行,因此尝试使用 pthread_setaffinity_np 不是正确的方法(如果您想更改 当前正在运行的线程的亲和力)。

但是... pthread_create 有一个属性参数(您将 NULL 传递给它)。这存储了您希望如何创建线程的信息。

亲和力是您可以通过该参数设置的属性之一。请参阅 pthread_attr_init 的手册页文档和 pthread_attr_setaffinity_np究竟如何

关于linux - CPU 亲和性掩码(将线程放在不同的 CPU 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2563442/

相关文章:

multithreading - JDK 1.4 的 Executor 和 Future 类等效项可实现执行超时

python - 使用 ThreadPoolExecutor 时 Django ORM 泄漏连接

C Pthread : Kill other threads gracefully through one thread

c - 减轻环形缓冲区轮询影响的策略

linux - pthread_self 返回一个大整数或 0,这取决于 libpthread 是否存在

ruby - 在 Linux Mint 上安装 Jekyll,从未被识别

linux - 解析命令输出

c++ - 调用 std::future::get() 后线程真的启动了吗?

linux - bash 'yes a' 和 'yes b'

java - 使用gcore生成的java corefile有用吗?