ein*_*ica 5 c multithreading pthreads
SO 上的帖子表明这pthread_t是一种不透明类型,不是数字,当然不是线程索引,您不应该直接比较pthread_t's 等。
问题:
为什么?是否真的打算支持没有线程数字 ID 的系统?当pthread_t实现很简单
typedef unsigned long int pthread_t;
Run Code Online (Sandbox Code Playgroud)
?
如何?上面一行之前有一个注释,所以它实际上是
/* Thread identifiers. The structure of the attribute type is not
   exposed on purpose.  */
typedef unsigned long int pthread_t;
Run Code Online (Sandbox Code Playgroud)
在pthreadtypes.h这是什么意思?什么属性类型?这不是某个全局线程表的索引吗?
POSIX 标准允许pthread_t更复杂的东西(例如结构)。请参阅上一个问题,尤其是@james-mcnellis 的回答。金钱报价:
应用 IEEE Std 1003.1-2001/Cor 2-2004,项目 XBD/TC2/D6/26,将 pthread_t 添加到不需要为算术类型的类型列表中,从而允许将 pthread_t 定义为结构。
更新:以下是一些更复杂pthread_t定义的示例:
这是pthread_tWin32 的 pthreads 库中使用的结构的古老(2007 年)理由:https : //sourceware.org/ml/pthreads-win32/2007/msg00056.html
是否真的打算支持没有线程数字 ID 的系统?
有不同的类型可以用作数字线程标识符。例如,在资源有限的系统上,可以使用 8 位线程标识符来代替unsigned long.
属性类型的结构不是故意公开的。
注释不是用于pthread_t定义,而是用于pthread_attr_t定义下面一行:
typedef union
{
  char __size[__SIZEOF_PTHREAD_ATTR_T];
  long int __align;
} pthread_attr_t;
Run Code Online (Sandbox Code Playgroud)
注释说明char __size[__SIZEOF_PTHREAD_ATTR_T]用于隐藏实际struct.
[
pthread_t]不是某个全局线程表的索引吗?
它不一定是。隐藏实际类型的事实允许实现者使用他希望的任何类型,包括指针或struct. 使用 astruct可以让实现者避免在他的库代码中使用全局线程表(不过,操作系统可能会保留这样的表)。