POSIX 线程中 pthread_create 的所有参数都非常容易理解,除了线程属性。什么是线程属性不应该被初始化的原因、方式和时间NULL?
我经历了Linuxman page http://man7.org/linux/man-pages/man3/pthread_create.3.html。我找到的描述线程属性 is:
-
Syntax:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void*),void *arg);
-
解释:
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.
这是非常不清楚的。我还用谷歌搜索了整个互联网,也没有在任何地方找到明确的解释。所以,当线程属性 is not NULL?
有人可以解释一下吗?高度赞赏所有评论和反馈。
您可以使用它来创建分离(不可连接)线程,或者将线程的堆栈大小设置为非默认值以及其他属性。
请参阅 POSIX 规范:
- pthread_attr_init() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_init.html
- pthread_attr_setdetachstate() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setdetachstate.html
- pthread_attr_setguardsize() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setguardsize.html
- pthread_attr_setinheritsched() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setinheritsched.html
- pthread_attr_setschedparam() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedparam.html
- pthread_attr_setschedpolicy() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setschedpolicy.html
- pthread_attr_setscope() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setscope.html
- pthread_attr_setstack() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setstack.html
- pthread_attr_setstacksize() http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_attr_setstacksize.html
(每个 URL 有两个函数 -pthread_attr_destroy()
和“get”类似于“set”功能。)
大多数情况下,您不需要修改这些。传递一个 NULL 指针到pthread_create()
相当于使用一组默认的属性——这就是pthread_attr_init()
为您创造。您可以在中更改您想要更改的属性pthread_attr_t
通过函数对象,然后将该修改后的对象传递给pthread_create()
反而。
另一件也没有明显理由的事情是第一个论点pthread_create
on pthread_t
数据类型定义。
所有 POSIX 线程类型都是不透明的——这是 POSIX 委员会经过深思熟虑的设计决定。您无法在类型内部随意查看。这使得实现更容易——你只能做函数允许你做的事情。最终,它也简化了程序员(用户)的生活;您不会被欺骗而使用不会迁移到其他系统的 POSIX 实现的内部知识。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)