48

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?

Skrud
  • 11,604
  • 5
  • 24
  • 22

5 Answers5

39

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.

BD at Rivenhill
  • 12,395
  • 10
  • 46
  • 49
Tom Alsberg
  • 6,873
  • 3
  • 28
  • 14
14

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;
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
jayadev
  • 3,576
  • 25
  • 13
  • `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`. – Eric Aug 15 '17 at 00:09
  • Thanks, that helps a lot while debugging multithreading in gdb and trying to match up `pthread_t` stored by the code with actual values shown by gdb in `info threads`. upd: actually, my bad, it turns out [I can just turn `pthread_t` into hex and see the value in `i thr`.](https://stackoverflow.com/a/2212329/2388257) – Hi-Angel Oct 13 '22 at 13:40
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.

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
2
pthread_t pthread_self()

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

Eric
  • 22,183
  • 20
  • 145
  • 196
1

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.

Kamil Zadora
  • 2,367
  • 6
  • 34
  • 43