Search code examples
pthreadssolarissetthreadaffinitymask

Obtain LWP id from a pthread_t on Solaris to use with processor_bind


On Solaris, processor_bind is used to set affinity for threads. You need to know the LWPID of the target thread or use the constant P_MYID to refer to yourself.

I have a function that looks like this:

void set_affinity(pthread_t thr, int cpu_number)
{
   id_t lwpid = what_do_I_call_here(thr);
   processor_bind(P_LWPID, lwpid, cpu_number, NULL);
}

In reality my function has a bunch of cross platform stuff in it that I've elided for clarity.

The key point is that I'd like to set the affinity of an arbitrary pthread_t so I can't use P_MYID.

How can I achieve this using processor_bind or an alternative interface?


Solution

  • Following up on this, and due to my confusion:

    The lwpid is what is created by

    pthread_create( &lwpid, NULL, some_func, NULL);
    

    Thread data is available externally to a process that is not the one making the pthread_create() call - via the /proc interface

    /proc/<pid>/lwp/<lwpid>/    lwpid == 1 is the main thread, 2 .. n are the lwpid in the above example.
    

    But this tells you almost nothing about which thread you are dealing with, except that it is the lwpid in the example above.

    /proc/pid/lwp/lwpid/lwpsinfo
    

    can be read into a struct lwpsinfo which has some more information, from which you might be able to ascertain if you are looking at the thread you want. see /usr/include/sys/procfs.h

    Or man -s 4 proc