0

我发现当我使用这样的东西时:

pthread_t thread_1, thread_2;
pthread_create (&thread_1, NULL, (void *) function_1, NULL);
pthread_create (&thread_2, NULL, (void *) function_2, NULL); 

thread_1 和thread_2 都在同一个逻辑CPU 中执行。

如何使它们都在不同的逻辑CPU中执行?

谢谢。

4

2 回答 2

0

pthread_setaffinity_np(3)允许您将线程的 CPU 关联掩码设置为 CPU 集。

例子:

int s, j;
cpu_set_t cpuset;
pthread_t thread;

thread = pthread_self();

/* Set affinity mask to include CPUs 0 to 7 */

CPU_ZERO(&cpuset); for (j = 0; j < 8; j++)
    CPU_SET(j, &cpuset);

s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0)
    handle_error_en(s, "pthread_setaffinity_np");

pthread_getaffinity_np返回线程的 CPU 关联掩码。

注意:此功能不可移植。

于 2013-11-15T08:24:04.147 回答
0
   int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
                              const cpu_set_t *cpuset);
   int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,
                              cpu_set_t *cpuset);

我认为这些功能可以帮助你。阅读本文档: 在此处输入链接描述

于 2013-11-15T08:28:10.367 回答