MacOSX: Scheduler affinity seems not to work

860 Views Asked by At

I tried to follow the suggestions in this blog, and it seems that, even if I'm setting the CPU Set to only one core of my MacOS, more of one thread is involved per time. Is it possible to make the thread run only in one processor on such Operative System? Thanks in advance.

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

1

There are 1 best solutions below

1
On

This question seems very similar to Is it possible to set pthread CPU affinity in OS X?; I believe the answers there also answer the questions here.