什么时候pthread_attr_t不是NULL?

Jes*_*hen 4 c multithreading posix arguments pthreads

除了pthread_attr_t之外,来自POSIX线程的pthread_create的所有参数都非常简单易懂.什么是pthread_attr_t,如何以及何时不应该由NULL初始化?

我浏览了Linux 手册页.我发现有关pthread_attr_t的描述是:

  • 句法:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void*),void *arg);
    
    Run Code Online (Sandbox Code Playgroud)
  • 说明:

    The attr argument points to a pthread_attr_t structure whose contents
    are used at thread creation time to determine attributes for the new
    thread; this structure is initialized using pthread_attr_init(3) and
    related functions.  If attr is NULL, then the thread is created with
    default attributes.
    
    Run Code Online (Sandbox Code Playgroud)

这是非常不清楚的.我也在互联网上搜索,也没有任何明确的解释.那么,当pthread_attr_t不为NULL时

有人可以对此有所了解吗?所有评论和反馈都非常感谢.

Jon*_*ler 7

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

请参阅POSIX规范:

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

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

不存在明显的理由或者另一件事,是从第一个参数pthread_createpthread_t的数据类型的定义.

所有POSIX线程类型都是不透明的 - 这是POSIX委员会经过深思熟虑的设计决策.你不能在这种类型内移动.这使得实现更容易 - 您只能执行功能允许您执行的操作.最终,它也简化了程序员(用户)的生活; 您不会被欺骗使用不会迁移到其他系统的POSIX实现的内部知识.