我如何从一个任意的pthread_t线程ID? [英] How do I get a thread ID from an arbitrary pthread_t?

查看:684
本文介绍了我如何从一个任意的pthread_t线程ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的pthread_t,我想要更改其CPU关联。问题是,我使用的glibc 2.3.2,它没有<一个href=\"http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_setaffinity_np.3.html\">pthread_setaffinity_np().这是确定的,但是,因为pthread_setaffinity_np()本身了sched_setaffinity()的包装,它可以通过传递被称为线程ID,而不是进程ID设置亲和力任意线程

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 ...线程ID说了sched_setaffinity可以工作是一个操作系统线程ID,就可以从的 gettid()系统调用。 这是从不透明类型的pthread_t ,然后gettid()的不同将只返回的当前线程的的线程ID。我需要能够设置任意线程的CPU亲和力

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.

不幸的是,我不能访问的pthread的私处,这将让我铸造的pthread_t到偷线程id的pthread结构* 。就更好了,我猜,因为依靠私人的实现是要求连的更多的麻烦。

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.

我也一直在 pthread_getunique_np 阅读了功能,然而这将返回唯一标识符积分。 - 我不相信这是任何方式或形式等效于一个操作系统线程ID

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.

因此​​,问题:我如何从一个任意的pthread_t获取线程ID

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

推荐答案

由于的pthread ■不要需要与Linux的线程(或内核线程在所有实施,这个问题),并且一些实现方式是完全用户级或混合,在的pthread s接口没有提供的功能来访问这些实施细节,因为这些将不会是便携式的(甚至横渡的pthread S于Linux实现)。使用这些线程库可以提供这作为一个扩展,但似乎没有任何这一点。

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.

除了访问线程库的内部数据结构(你可以理解的不想要的,虽然与你的处理器亲和Linux线程ID,你的code将无法移植反正假设),你可能能够在创建时捉弄,如果​​你控制code创建线程:

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:

在pthread_create()入口函数调用 gettid()(这由你有可能的方式必须使用系统调用宏直接,因为它并不总是由的libc 导出)做的,存储结果显山露水,然后调用原始的入口函数。如果你有相同的入口函数的多个线程,你可以传递一个递增的指针进入了 ARG 参数在pthread_create ,这将被传递到您创建以存储线程ID的入口函数。存储在pthread_create 的pthread_t 返回值C $ C>以相同的顺序,然后你就可以查找您创建献出的pthread_t 值的所有线程的Linux的线程ID。

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.

不管这招是值得的,取决于CPU亲和力多么重要的设置是在你的情况,对未访问的线程库的内部结构或取决于线程库,提供 pthread_setaffinity_np

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.

这篇关于我如何从一个任意的pthread_t线程ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆