Linux pthread_self() 和 gettid() 有什么区别?我应该使用哪一种?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6372102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:34:28  来源:igfitidea点击:

What is the difference between pthread_self() and gettid()? Which one should I use?

linuxpthreadsaffinity

提问by roelf

I'm trying to set the CPU affinity of threads on Linux. I'd like to know which one of the following approaches is recommended:

我正在尝试在 Linux 上设置线程的 CPU 亲和性。我想知道推荐以下哪一种方法:

  1. Get thread id using pthread_self()

    Set CPU affinity using pthread_setaffinity_np(....) by passing the thread id as an argument

  2. Get thread id using the gettid() call

    Set CPU affinity using sched_setaffinity(....) by passing the thread id in the place of the process id

  1. 使用 pthread_self() 获取线程 ID

    通过将线程 id 作为参数传递,使用 pthread_setaffinity_np(....) 设置 CPU 关联性

  2. 使用 gettid() 调用获取线程 ID

    使用 sched_setaffinity(....) 通过传递线程 id 代替进程 id 来设置 CPU 关联性

P.S: After setting the CPU affinity, I intend to increase the scheduling priority of the thread.

PS:设置CPU亲和度后,打算提高线程的调度优先级。

回答by cnicutar

They are not the same. Here are some bits I collected from TLPI(I couldn't find a large-enough block that completely describes this). If you're in a hurry you probably want just the last part.

他们不一样。以下是我从TLPI收集的一些内容(我找不到足够大的块来完整描述这一点)。如果你赶时间,你可能只想要最后一部分。

gettid

gettid

Linux 2.4 introduced a new system call, gettid(), to allow a thread to obtain its own thread ID.

Linux 2.4 引入了一个新的系统调用 ,gettid()以允许线程获取自己的线程 ID。

Each thread within a thread group is distinguished by a unique thread identifier. A thread ID is represented using the same data type that is used for a process ID, pid_t. Thread IDs are unique system-wide, and the kernel guarantees that no thread ID will be the same as any process ID on the system, except when a thread is the thread group leader for a process.

线程组中的每个线程都由唯一的线程标识符来区分。线程 ID 使用与进程 ID 相同的数据类型表示pid_t。线程 ID 在系统范围内是唯一的,内核保证没有线程 ID 与系统上的任何进程 ID 相同,除非线程是进程的线程组领导者。

pthread_self

pthread_self

Each thread within a process is uniquely identified by a thread ID. A thread can obtain its own ID using pthread_self().

进程内的每个线程都由线程 ID 唯一标识。线程可以使用 获取自己的 ID pthread_self()

The pthread_equal()function is needed to compare thread ids because the pthread_tdata type must be treated as opaque data.

pthread_equal()需要该函数来比较线程 ID,因为pthread_t必须将数据类型视为不透明数据

In the Linux threading implementations, thread IDs are unique across processes. However, this is not necessarily the case on other implementations, and SUSv3 explicitly notes that an application can't portably use a thread ID to identify a thread in another process.

在 Linux 线程实现中,线程 ID 在进程间是唯一的。但是,在其他实现中不一定如此,SUSv3 明确指出应用程序不能可移植地使用线程 ID 来标识另一个进程中的线程

gettidvs pthread_self

gettid对比 pthread_self

POSIX thread IDs are not the same as the thread IDs returned by the Linux-specific gettid()system call. POSIX thread IDs are assigned and maintained by the threading implementation. The thread ID returned by gettid()is a number (similar to a process ID) that is assigned by the kernel.

POSIX 线程 ID 与 Linux 特定gettid()系统调用返回的线程 ID 不同。POSIX 线程 ID 由线程实现分配和维护。返回的线程IDgettid()是内核分配的一个数字(类似于进程ID)。

I would go with pthread_setaffinity_npbut be aware that the manual says:

我会去,pthread_setaffinity_np但要注意手册上说:

These functions are implemented on top of the sched_setaffinity(2)

这些函数是在 sched_setaffinity(2) 之上实现的

回答by user666412

I believe the fact that gettid()exists only as a system call and hasn't been exposed directly as an API call could mean "use it only if you're absolutely certain of what you're doing" and gettid()is not meant to be portable.

我相信gettid()仅作为系统调用存在并且没有直接作为 API 调用公开的事实可能意味着“只有在您绝对确定自己在做什么时才使用它”并且gettid()并不意味着可移植。

You should be better if you stick to pthread. You can change scheduling policy/priority later with pthread_setschedparam()

如果你坚持,你应该会更好pthread。您可以稍后更改调度策略/优先级pthread_setschedparam()