读书人

怎么确定 pthread 双线程是在两个核下

发布时间: 2012-09-27 11:11:17 作者: rapoo

如何确定 pthread 双线程是在两个核上运行,而不是在一个核(超核)上运行
RT,是否有神马控制语句

[解决办法]

C/C++ code
       In the following program, the main thread uses pthread_setaffinity_np() to set its CPU affinity mask to include CPUs 0 to 7 (which may not all be avail-       able on the system), and then calls pthread_getaffinity_np() to check the resulting CPU affinity mask of the thread.       #define _GNU_SOURCE       #include <pthread.h>       #include <stdio.h>       #include <stdlib.h>       #include <errno.h>       #define handle_error_en(en, msg) \               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)       int       main(int argc, char *argv[])       {           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");           /* Check the actual affinity mask assigned to the thread */           s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);           if (s != 0)               handle_error_en(s, "pthread_getaffinity_np");           printf("Set returned by pthread_getaffinity_np() contained:\n");           for (j = 0; j < CPU_SETSIZE; j++)               if (CPU_ISSET(j, &cpuset))                   printf("    CPU %d\n", j);           exit(EXIT_SUCCESS);       } 

读书人网 >UNIXLINUX

热点推荐