为什么以及在什么意义上 pthread_t 是不透明类型? [英] Why and in what sense is pthread_t an opaque type?
问题描述
SO 上的帖子表明 pthread_t
是一种不透明类型,不是数字,当然也不是线程索引,您不应该直接比较 pthread_t
,等等等等
Posts here on SO suggest that pthread_t
is an opaque type, not a number, certainly not a thread index, that you shouldn't directly compare pthread_t
's, etc. etc.
问题:
为什么?是否真的打算支持没有线程数字 ID 的系统?当
pthread_t
实现只是
typedef unsigned long int pthread_t;
?
怎么样?上面一行之前有一个注释,所以它实际上是
How? There's a comment before the above line, so it's actually
/* Thread identifiers. The structure of the attribute type is not
exposed on purpose. */
typedef unsigned long int pthread_t;
在pthreadtypes.h
中是什么意思?什么属性类型?这不是某个全局线程表的索引吗?
in pthreadtypes.h
what does that mean? What attribute type? Isn't this an index into some global table of threads?
推荐答案
是否真的打算支持没有线程数字 ID 的系统?
Is there really the intent to support systems with no numeric IDs for threads?
有不同的类型可以用作数字线程标识符.例如,在资源有限的系统上,可以使用 8 位线程标识符代替 unsigned long
.
There are different types that could serve as numeric thread identifier. For example, on systems with limited resources an 8-bit thread identifier could be used instead of unsigned long
.
属性类型的结构不是故意暴露的.
The structure of the attribute type is not exposed on purpose.
注释不是针对pthread_t
定义,而是针对pthread_attr_t
定义下面一行:
The comment is not for pthread_t
definition, but for the pthread_attr_t
definition one line below:
typedef union
{
char __size[__SIZEOF_PTHREAD_ATTR_T];
long int __align;
} pthread_attr_t;
注释指出 char __size[__SIZEOF_PTHREAD_ATTR_T]
用于隐藏实际 struct
的内容.
The comment states that char __size[__SIZEOF_PTHREAD_ATTR_T]
is used in order to hide the content of the actual struct
.
[pthread_t
] 不是某个全局线程表的索引吗?
Isn't [
pthread_t
] an index into some global table of threads?
不必如此.隐藏实际类型的事实允许实现者使用他希望的任何类型,包括指针或 struct
.使用 struct
可以让实现者避免在他的库代码中使用全局线程表(不过,操作系统可能会保留这样的表).
It does not have to be. The fact that the actual type is hidden allows an implementer to use any type that he wishes, including a pointer or a struct
. Using a struct
lets the implementer avoid using a global table of threads in the code of his library (OS would probably keep such table, though).
这篇关于为什么以及在什么意义上 pthread_t 是不透明类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!