2016-04-08 76 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