当 pthread_attr_t 不为 NULL 时?

when pthread_attr_t is not NULL?

来自 POSIX 个线程的 pthread_create 的所有参数都非常容易理解,除了 pthread_attr_t. pthread_attr_t 是什么,应该如何以及何时不由 [=26= 初始化]NULL?

我经历了 Linux man page。我找到的关于 pthread_attr_t 的描述是:

这很不清楚。我也在整个互联网上用谷歌搜索,也没有在任何地方找到明确的解释。所以,当 pthread_attr_t 不是 NULL?

有人可以解释一下吗?非常感谢所有评论和反馈。

您可以使用它来创建分离的(不可连接的)线程,或将线程的堆栈大小设置为非默认值,以及其他属性。

查看 POSIX 规范:

(每个 URL 有两个函数 — pthread_attr_destroy() 和 'get' 类似于 'set' 函数。)

大多数情况下,您不需要修改这些。将 NULL 指针传递给 pthread_create() 相当于使用一组默认属性 — 这是 pthread_attr_init() 为您创建的。您可以通过函数更改 pthread_attr_t 对象中您希望更改的属性,然后将修改后的对象传递给 pthread_create()

Another thing that there is no apparent justification either, is the first argument from pthread_create on pthread_t data type definition.

所有 POSIX 线程类型都是不透明的 — 这是 POSIX 委员会经过深思熟虑的设计决定。你不能在可移植的类型中四处闲逛。这使得实现起来更容易——你只能做函数允许你做的事情。最终,它也简化了程序员(用户)的生活;您不会被骗去使用不会迁移到其他系统的 POSIX 实现的内部知识。