0

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

jackb
  • 695
  • 8
  • 26

1 Answers1

0

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.

Community
  • 1
  • 1
Erik Schnetter
  • 482
  • 4
  • 8
  • No. I am asking if the behaviour of the code you pointed to is correct, since it seems that more than one cores run the same pthread, and hence the affinity of a single core seems not to work through the provided apis. – jackb Apr 08 '16 at 17:43