2013-11-15 46 views
0

在不同的逻辑CPU执行我发现,当我使用的是这样的:如何使不同的线程使用并行线程

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两者的执行?

谢谢。

+0

他们都准备好,并在即使另一个核心是免费的相同内核上运行? –

+0

@马丁詹姆斯是的。当其他内核空闲时,它们都运行在同一个内核中。我不知道。 – user2967915

回答

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亲和力掩码。

注:这个函数是不可移植。

+0

我的操作系统是windows NT,编译器是gcc-mingw32。能否成功编译'pthread_getaffinity_np'? – user2967915

+0

@ user2967915我想不是。在Windows中,你有'SetThreadAffinityMask'和'SetProcessAffinityMask'(http://en.wikipedia.org/wiki/Processor_affinity#Specific_operating_systems) – HAL

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); 

我觉得这些功能可以帮助你。阅读本文件: enter link description here

+0

我的操作系统是Windows NT和编译器是gcc-的mingw32。 pthread_getaffinity_np可以编译成功吗? – user2967915

+0

@ user2967915我认为你不能成功。只有在Linux系统中才能使用这些函数,而不是在gcc中。但你可以试试,祝你好运。 – BlackMamba