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?