How do I get a thread ID from an arbitrary pthread_t?

81,882

Solution 1

Since pthreads do not need to be implemented with Linux threads (or kernel threads at all, for that matter), and some implementations are entirely user-level or mixed, the pthreads interface does not provide functions to access these implementation details, as those would not be portable (even across pthreads implementations on Linux). Thread libraries that use those could provide this as an extension, but there do not seem to be any that do.

Other than accessing internal data structures of the threading library (which you understandably do not want, although with your assumptions about processor affinity and Linux thread IDs, your code will not be portable anyway), you may be able to play a trick at creation time, if you control the code that creates the threads:

Give pthread_create() an entry function that calls gettid() (which by the way you are likely to have to do using the syscall macro directly because it is not always exported by libc), stores the result somewhere, and then calls the original entry function. If you have multiple threads with the same entry function, you can pass an incremented pointer into an array in the arg argument to pthread_create, which will then be passed to the entry function you created to store the thread ID in. Store the pthread_t return value of pthread_create in the same order, and then you will be able to look up the Linux thread IDs of all threads you created given their pthread_t value.

Whether this trick is worth it, depends on how important setting the CPU affinity is in your case, versus not accessing internal structures of the thread library or depending on a thread library that provides pthread_setaffinity_np.

Solution 2

Actually pthread_self returns pthread_t and not a integer thread id you can work with, the following helper function will get you that in a portable way across different POSIX systems.

uint64_t gettid() {
    pthread_t ptid = pthread_self();
    uint64_t threadId = 0;
    memcpy(&threadId, &ptid, std::min(sizeof(threadId), sizeof(ptid)));
    return threadId;
}

Solution 3

In glibc 2.24 the pthread_t returned is just the pointer to an opaque struct pthread. You can look up the definition in nptl/descr.h.

Solution 4

pthread_t pthread_self()

this return current pthread_t, which is thread id, you can convert it to type "unsigned int",

Solution 5

I would suggest a simple workaround with a shared int array where you could write the thread id from the threads to access it later.

Hope that helps.

Share:
81,882

Related videos on Youtube

Skrud
Author by

Skrud

Updated on September 22, 2020

Comments

  • Skrud
    Skrud over 3 years

    I have a pthread_t, and I'd like to change its CPU affinity. The problem is that I'm using glibc 2.3.2, which doesn't have pthread_setaffinity_np(). That's OK, though, because pthread_setaffinity_np() is itself a wrapper of sched_setaffinity(), which can be called by passing a thread ID instead of a process ID to set the affinity for an arbitrary thread.

    BUT ... The thread id that sched_setaffinity can work with is an OS thread id, the kind that you can get from the gettid() system call. This is different from the opaque type pthread_t, and gettid() will only return the thread-id of the current thread. I need to be able to set the CPU affinity of an arbitrary thread.

    Unfortunately, I can't access the pthread's private parts, which would let me steal the thread id by casting a pthread_t to a struct pthread *. All the better, I guess, since relying on private implementations is asking for even more trouble.

    I've also been reading up on the pthread_getunique_np function, however this returns a "unique integral identifier" -- which I don't believe is in any way shape or form equivalent to an OS thread id.

    Hence, the question: How can I get a thread ID from an arbitrary pthread_t?

  • raimue
    raimue about 15 years
    There is no need for mmap, threads share the same memory anyway.
  • JumpAlways
    JumpAlways over 7 years
    Actually unsigned long int.
  • Eric
    Eric over 6 years
    pthread_t tid = pthread_self(); works on the main thread, but not on another? I have my exe crash calling that from __start_routine after calling pthread_create.
  • ivan.ukr
    ivan.ukr over 5 years
    pthread_self returns something like address of internal structure used by pthreads. This is not OS assigned thread ID returned by system call gettid().