当pthread_attr_t不为NULL时? [英] when pthread_attr_t is not NULL?
问题描述
除了 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);
-
说明:
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.
这还不清楚.我也在整个互联网上搜索,也没有清晰的解释.因此,当 pthread_attr_t 不是 NULL 时?
有人可以说明一下吗?高度赞赏所有评论和反馈.
除其他属性外,您可以使用它来创建分离的(不可联接)线程,或将线程的堆栈大小设置为非默认值.
有关以下内容,请参见POSIX规范:
-
pthread_attr_init()
-
pthread_attr_setdetachstate()
-
pthread_attr_setguardsize()
-
pthread_attr_setinheritsched()
-
pthread_attr_setschedparam()
-
pthread_attr_setschedpolicy()
-
pthread_attr_setscope()
-
pthread_attr_setstack()
-
pthread_attr_setstacksize()
(每个URL有两个功能-pthread_attr_destroy()
和"set"功能的"get"类似物.)
通常,您不需要修改这些内容.将NULL指针传递给pthread_create()
等效于使用默认属性集-这是pthread_attr_init()
为您创建的属性.您可以通过函数在pthread_attr_t
对象中更改希望更改的属性,然后将该修改后的对象传递给pthread_create()
.
没有明显理由的另一件事是
pthread_t
数据类型定义中pthread_create
的第一个参数.
所有POSIX线程类型都是不透明的-这是POSIX委员会的蓄意设计决定.您不能随便在类型内部进行戳戳.这使实现更容易-您只能执行功能允许执行的操作.最终,它也简化了程序员(用户)的生活.您不会被欺骗使用不会迁移到其他系统的POSIX实现的内部知识.
All parameters for pthread_create from POSIX threads are pretty straightforward to understand, except pthread_attr_t. What is pthread_attr_t for, how, and when it is supposed to be not initialized by NULL?
I went through the Linux man page. The description I found about pthread_attr_t is:
Syntax:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void*),void *arg);
Explanation:
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.
Which is very unclear. I also googled all over the internet, and no clear explanation to be found anywhere either. So, when pthread_attr_t is not NULL?
Can someone please shed some light about that? All comments and feedback are highly appreciated.
You can use it to created a detached (non-joinable) thread, or to set the thread's stack size to a non-default value, amongst other attributes.
See the POSIX specifications for:
pthread_attr_init()
pthread_attr_setdetachstate()
pthread_attr_setguardsize()
pthread_attr_setinheritsched()
pthread_attr_setschedparam()
pthread_attr_setschedpolicy()
pthread_attr_setscope()
pthread_attr_setstack()
pthread_attr_setstacksize()
(There are two functions per URL — pthread_attr_destroy()
and 'get' analogues to the 'set' functions.)
Most often, you don't need to modify these. Passing a NULL pointer to pthread_create()
is equivalent to using a default set of attributes — which is what pthread_attr_init()
creates for you. You can change the attributes that you wish to change in the pthread_attr_t
object via the functions and then pass that modified object to pthread_create()
instead.
Another thing that there is no apparent justification either, is the first argument from
pthread_create
onpthread_t
data type definition.
All the POSIX thread types are opaque — that was a deliberate design decision by the POSIX committees. You can't poke around inside the type portably. This makes it easier for the implementation — you can only do what the functions allow you to do. Ultimately, it simplifies life for the programmers (users) too; you don't get tricked into using internal knowledge of the POSIX implementation that won't migrate to other systems.
这篇关于当pthread_attr_t不为NULL时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!