Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get a thread ID from an arbitrary pthread_t?
    text
    copied!<p>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 <a href="http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_setaffinity_np.3.html" rel="noreferrer">pthread_setaffinity_np()</a>. That's OK, though, because pthread_setaffinity_np() is itself a wrapper of <a href="http://linux.die.net/man/2/sched_setaffinity" rel="noreferrer">sched_setaffinity()</a>, which can be called by passing a thread ID instead of a process ID to set the affinity for an arbitrary thread.</p> <p><strong>BUT</strong> ... The thread id that sched_setaffinity can work with is an OS thread id, the kind that you can get from the <a href="http://linux.die.net/man/2/gettid" rel="noreferrer">gettid()</a> system call. <strong>This is different from the opaque type pthread_t</strong>, and gettid() will only return the thread-id of the <em>current thread</em>. I need to be able to set the CPU affinity of an arbitrary thread.</p> <p>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 <code>struct pthread *</code>. All the better, I guess, since relying on private implementations is asking for even <em>more</em> trouble.</p> <p>I've also been reading up on the <a href="http://cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-23.htm" rel="noreferrer">pthread_getunique_np</a> 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.</p> <p>Hence, the question: How can I get a thread ID from an arbitrary pthread_t?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload