为什么以及在什么意义上 pthread_t 是不透明类型?
Why and in what sense is pthread_t an opaque type?
SO 上的帖子表明 pthread_t
是不透明类型,不是数字,当然也不是线程索引,您不应该直接比较 pthread_t
等等。
问题:
为什么?是否真的有意支持没有线程数字 ID 的系统?当 pthread_t
实现只是
typedef unsigned long int pthread_t;
?
怎么样?上面一行之前有注释,所以实际上是
/* Thread identifiers. The structure of the attribute type is not
exposed on purpose. */
typedef unsigned long int pthread_t;
在pthreadtypes.h
中是什么意思?什么属性类型?这不是某个全局 table 线程的索引吗?
POSIX 标准允许 pthread_t
是更复杂的东西(例如结构)。 See this previous question,尤其是@james-mcnellis 的回答。货币报价:
IEEE Std 1003.1-2001/Cor 2-2004, item XBD/TC2/D6/26 is applied, adding
pthread_t to the list of types that are not required to be arithmetic
types, thus allowing pthread_t to be defined as a structure.
更新:这里有一些更复杂的 pthread_t
定义的例子:
- https://github.com/JoakimSoderberg/pthreads-win32/blob/master/pthread.h
- http://www.mit.edu/afs.new/sipb/project/pthreads/include/pthread.h
- http://svn.gna.org/svn/xenomai/trunk/include/posix/pthread.h
这是 Win32 的 pthreads 库中使用的 pthread_t
结构的古老 (2007) 证明:https://sourceware.org/ml/pthreads-win32/2007/msg00056.html
Is there really the intent to support systems with no numeric IDs for threads?
有多种类型可以用作数字线程标识符。例如,在资源有限的系统上,可以使用 8 位线程标识符代替 unsigned long
.
The structure of the attribute type is not exposed on purpose.
对于 pthread_t
定义,评论是 而不是 ,但对于 pthread_attr_t
定义,下面一行:
typedef union
{
char __size[__SIZEOF_PTHREAD_ATTR_T];
long int __align;
} pthread_attr_t;
评论说char __size[__SIZEOF_PTHREAD_ATTR_T]
是为了隐藏实际struct
的内容。
Isn't [pthread_t
] an index into some global table of threads?
不必如此。实际类型是隐藏的这一事实允许实现者使用他希望的任何类型,包括指针或 struct
。使用 struct
可以让实施者避免在他的库代码中使用全局 table 线程(不过 OS 可能会保留这样的 table)。
SO 上的帖子表明 pthread_t
是不透明类型,不是数字,当然也不是线程索引,您不应该直接比较 pthread_t
等等。
问题:
为什么?是否真的有意支持没有线程数字 ID 的系统?当
pthread_t
实现只是typedef unsigned long int pthread_t;
?
怎么样?上面一行之前有注释,所以实际上是
/* Thread identifiers. The structure of the attribute type is not exposed on purpose. */ typedef unsigned long int pthread_t;
在
pthreadtypes.h
中是什么意思?什么属性类型?这不是某个全局 table 线程的索引吗?
POSIX 标准允许 pthread_t
是更复杂的东西(例如结构)。 See this previous question,尤其是@james-mcnellis 的回答。货币报价:
IEEE Std 1003.1-2001/Cor 2-2004, item XBD/TC2/D6/26 is applied, adding pthread_t to the list of types that are not required to be arithmetic types, thus allowing pthread_t to be defined as a structure.
更新:这里有一些更复杂的 pthread_t
定义的例子:
- https://github.com/JoakimSoderberg/pthreads-win32/blob/master/pthread.h
- http://www.mit.edu/afs.new/sipb/project/pthreads/include/pthread.h
- http://svn.gna.org/svn/xenomai/trunk/include/posix/pthread.h
这是 Win32 的 pthreads 库中使用的 pthread_t
结构的古老 (2007) 证明:https://sourceware.org/ml/pthreads-win32/2007/msg00056.html
Is there really the intent to support systems with no numeric IDs for threads?
有多种类型可以用作数字线程标识符。例如,在资源有限的系统上,可以使用 8 位线程标识符代替 unsigned long
.
The structure of the attribute type is not exposed on purpose.
对于 pthread_t
定义,评论是 而不是 ,但对于 pthread_attr_t
定义,下面一行:
typedef union
{
char __size[__SIZEOF_PTHREAD_ATTR_T];
long int __align;
} pthread_attr_t;
评论说char __size[__SIZEOF_PTHREAD_ATTR_T]
是为了隐藏实际struct
的内容。
Isn't [
pthread_t
] an index into some global table of threads?
不必如此。实际类型是隐藏的这一事实允许实现者使用他希望的任何类型,包括指针或 struct
。使用 struct
可以让实施者避免在他的库代码中使用全局 table 线程(不过 OS 可能会保留这样的 table)。