什么是“属性”? pthread 互斥锁?
函数 pthread_mutex_init 允许您指定指向属性的指针。但我还没有找到关于 pthread 属性是什么的很好的解释。我总是只提供 NULL。这个论证有什么用吗?
对于那些忘记它的人来说,该文档:
PTHREAD_MUTEX_INIT(3) BSD 库 功能手册
PTHREAD_MUTEX_INIT(3)姓名 pthread_mutex_init -- 创建互斥锁
概要
#include
; 整数 pthread_mutex_init(pthread_mutex_t *限制互斥体, const pthread_mutexattr_t *限制 attr); 描述 pthread_mutex_init() 函数创建一个新的互斥体,具有属性 指定的 与属性。如果 attr 为 NULL,则使用默认属性。
The function pthread_mutex_init allows you to specify a pointer to an attribute. But I have yet to find a good explanation of what pthread attributes are. I have always just supplied NULL. Is there a use to this argument?
The documentation, for those of you who forget it:
PTHREAD_MUTEX_INIT(3) BSD Library
Functions Manual
PTHREAD_MUTEX_INIT(3)NAME
pthread_mutex_init -- create a mutexSYNOPSIS
#include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
DESCRIPTION
The pthread_mutex_init() function creates a new mutex, with attributes
specified
with attr. If attr is NULL, the default attributes are used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查找该信息的最佳位置是 POSIX 标准页面。
NULL
互斥体属性为您提供了实现定义的默认属性。如果您想了解可以使用属性做什么,请查看 请参阅以下参考,并点击另请参阅
部分中的pthread_mutexattr_*
链接。通常,默认值是一组合理的属性,但它可能因平台而异,因此我更喜欢显式创建具有已知属性的互斥体(更好的可移植性)。这是标准 1003.1-2008 的第 7 期。起点是此处。单击左下角的
Headers
将允许您导航到特定功能(包括pthreads.h
)。这些属性允许您设置或获取:
而且,为了完整起见,还有 init 和 destroy 调用,与特定属性不直接相关,但用于创建它们。
The best place to find that information is from the POSIX standards pages.
A
NULL
mutex attribute gives you an implementation defined default attribute. If you want to know what you can do with attributes, check out the following reference and follow thepthread_mutexattr_*
links in theSEE ALSO
section. Usually, the default is a sensible set of attributes but it may vary between platforms, so I prefer to explicitly create mutexes with known attributes (better for portability).This is for issue 7 of the standard, 1003.1-2008. The starting point for that is here. Clicking on
Headers
in the bottom left will allow you to navigate to the specific functionality (includingpthreads.h
).The attributes allow you to set or get:
And, for completeness, there's the init and destroy calls as well, not directly related to a specific attribute but used to create them.
所有互斥体属性均通过以下形式的函数在互斥体属性对象中设置:
所有互斥体属性均通过以下形式的函数从互斥体属性对象中检索:
其中名称和类型定义如下表所示:
All mutex attributes are set in a mutex attribute object by a function of the form:
All mutex attributes are retrieved from a mutex attribute object by a function of the form:
where name and Type are defined as in the table below:
如果向下滚动
,你会发现一堆
pthread_mutexattr_...
函数,包括init
、destroy
和设置各种函数互斥体的属性。当您传递NULL
时,将使用所有这些属性的合适默认值创建互斥锁,但如果您需要修改特定属性,您可以构造一个pthread_mutexattr_t
结构并将其传入。If you scroll down the function listing for
<pthread.h>
, you will find a bunch ofpthread_mutexattr_...
functions, including aninit
,destroy
and functions to set various attributes of a mutex. When you passNULL
, the mutex is created with suitable defaults for all these attributes, but if you need to modify specific attributes, you can construct apthread_mutexattr_t
structure and pass it in.将此参数应用 NULL 意味着使用默认参数。
因此,由于某些原因,您可能想要更改这些默认设置(使用 pthread_mutexattr_init)。
该文档解释了您需要的有关这些互斥体设置的所有信息。
Applying NULL to this argument implies using the default argument.
So for some reasons you could want to change these default settings (using pthread_mutexattr_init).
The documentation explains all you need about these mutex settings.