2016-04-08 77 views
0

我试图按照this博客中的建议,并且似乎即使我将CPU设置为只有一个MacOS核心,每个线程涉及更多时间。是否有可能使线程只能在这种操作系统上的一个处理器上运行?提前致谢。MacOSX:调度程序亲和力似乎不工作

void *th_func(void *arg); 

pthread_t thread; //the thread 
int counted = 0; 

void start() { 
    int* n = (int*)malloc(sizeof(int)); 
    *n = 0; 
    printf("creating on %d.\n",n[0]); 
    pthread_create(&thread,NULL,th_func,((void*) n)); 
} 

void waitall() { 
    pthread_join(thread,NULL); 
} 

int main(int argc, char** args) { 
start(); 
    waitall(); 
return 0; 
} 


void *th_func(void *arg) 
{ 
    cpu_set_t cpuset; 
    int cpu = ((int*)arg)[0]; 
    CPU_ZERO(&cpuset); 
    CPU_SET(cpu , &cpuset); 
    pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset); 
    printf("Start suffocating on %d.\n",cpu); 
    while(1) { 

    }; 
} 

enter image description here

回答

0

这个问题似乎很相似,Is it possible to set pthread CPU affinity in OS X?;我相信那里的答案也回答了这里的问题。

+0

不,我问你指出的代码行为是否正确,因为看起来不止一个内核运行相同的线程,因此单个内核的亲和性似乎不能通过提供的apis工作。 – jackb