pthread_self()和gettid()有什么区别?我应该使用哪一个? [英] What is the difference between pthread_self() and gettid()? Which one should I use?

查看:465
本文介绍了pthread_self()和gettid()有什么区别?我应该使用哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Linux上设置线程的CPU相似性.我想知道推荐以下哪种方法:

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:

使用pthread_self()获取线程ID

Get thread id using pthread_self()

通过将线程ID作为参数传递,使用pthread_setaffinity_np(....)设置CPU亲和力

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

  • 使用gettid()调用获取线程ID

    Get thread id using the gettid() call

    使用sched_setaffinity(....)设置CPU亲和力,方法是将线程ID传递到进程ID处

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

  • P.S:设置CPU关联后,我打算增加线程的调度优先级.

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

    推荐答案

    它们不相同.以下是我从 TLPI 收集的一些信息(我找不到大型的足以完全描述这一点的块).如果您着急的话,可能只需要最后一部分.

    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.

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

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

    线程组中的每个线程都由唯一的线程标识符来区分.线程ID使用与进程ID pid_t相同的数据类型表示.线程ID在系统范围内是唯一的,并且内核保证,除非线程是进程的线程组负责人,否则线程ID不会与系统上的任何进程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唯一标识.线程可以使用pthread_self()获得其自己的ID.

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

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

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

    在Linux线程实现中,线程ID在进程之间是唯一的.但是,在其他实现上不一定是这种情况, SUSv3明确指出,应用程序无法可移植地使用线程ID来标识另一个进程中的线程.

    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.

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

    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.

    我会选择pthread_setaffinity_np,但是请注意,该手册说:

    I would go with pthread_setaffinity_np but be aware that the manual says:

    这些功能在顶部实现 sched_setaffinity(2)的

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

    这篇关于pthread_self()和gettid()有什么区别?我应该使用哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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