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亲和性。我想知道推荐以下哪种方法:
-
Get thread id using pthread_self()
使用pthread_self()获取线程ID
Set CPU affinity using pthread_setaffinity_np(....) by passing the thread id as an argument
通过将线程id作为参数传递,使用pthread_setaffinity_np(....)设置CPU关联
-
Get thread id using the gettid() call
使用gettid()调用获取线程ID
Set CPU affinity using sched_setaffinity(....) by passing the thread id in the place of the process id
通过在进程ID的位置传递线程id,使用sched_setaffinity(....)设置CPU关联
P.S: After setting the CPU affinity, I intend to increase the scheduling priority of the thread.
P.S:设置CPU亲和性后,我打算增加线程的调度优先级。
2 个解决方案
#1
19
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
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 pid_t相同的数据类型表示线程ID。线程ID在系统范围内是唯一的,并且内核保证没有线程ID与系统上的任何进程ID相同,除非线程是进程的线程组负责人。
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唯一标识。线程可以使用pthread_self()获取自己的ID。
The pthread_equal()
function is needed to compare thread ids because the pthread_t
data type must be treated as opaque data.
比较线程ID需要pthread_equal()函数,因为必须将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来标识另一个进程中的线程。
gettid
vs 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由线程实现分配和维护。 gettid()返回的线程ID是由内核分配的数字(类似于进程ID)。
I would go with pthread_setaffinity_np
but be aware that the manual says:
我会选择pthread_setaffinity_np,但请注意手册说:
These functions are implemented on top of the sched_setaffinity(2)
这些函数在sched_setaffinity(2)之上实现
#2
0
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()更改调度策略/优先级
#1
19
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
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 pid_t相同的数据类型表示线程ID。线程ID在系统范围内是唯一的,并且内核保证没有线程ID与系统上的任何进程ID相同,除非线程是进程的线程组负责人。
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唯一标识。线程可以使用pthread_self()获取自己的ID。
The pthread_equal()
function is needed to compare thread ids because the pthread_t
data type must be treated as opaque data.
比较线程ID需要pthread_equal()函数,因为必须将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来标识另一个进程中的线程。
gettid
vs 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由线程实现分配和维护。 gettid()返回的线程ID是由内核分配的数字(类似于进程ID)。
I would go with pthread_setaffinity_np
but be aware that the manual says:
我会选择pthread_setaffinity_np,但请注意手册说:
These functions are implemented on top of the sched_setaffinity(2)
这些函数在sched_setaffinity(2)之上实现
#2
0
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()更改调度策略/优先级