当 pthread_attr_t 不为 NULL 时? [英] when pthread_attr_t is not NULL?

查看:21
本文介绍了当 pthread_attr_t 不为 NULL 时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我浏览了 Linux 手册页.我找到的关于 pthread_attr_t 的描述是:

I went through the Linux man page. The description I found about pthread_attr_t is:

  • 语法:

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.
    

  • 这是非常不清楚的.我还在互联网上搜索了所有内容,也找不到任何明确的解释.那么,什么时候 pthread_attr_t 不是 NULL ?

    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.

    查看 POSIX 规范:

    See the POSIX specifications for:

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

    (There are two functions per URL — pthread_attr_destroy() and 'get' analogues to the 'set' functions.)

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

    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.

    另一件事也没有明显的理由,是 pthread_createpthread_t 数据类型定义的第一个参数.

    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 实现的内部知识.

    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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆