c++cpthreadssetthreadaffinitymask

Pthread_setaffinity_np invalid argument


I am trying to change the affinity of each thread inside the start routine.

Here's the code. In the below code, 't' is the argument passed through the pthread_create function.

    cpu_set_t mask;
    pthread_t c;

    a = *((int *)t);

    printf(" thread no. is %d \n",a);

    CPU_ZERO(&mask);

    a =a/2 + 1;

    CPU_SET(a,&mask);

    c=pthread_self();
    s=pthread_setaffinity_np(c,sizeof(cpu_set_t), &mask);
    if (s!=0)
        handle_error_en(s,"pthread_setaffinity_np");

The thread affinity is not getting changed. Where am I going wrong?


Solution

  • I had misunderstood the bounds of the mask. That was where I was going wrong.