什么是“属性”? pthread 互斥锁?

发布于 2024-10-03 12:39:43 字数 537 浏览 4 评论 0原文

函数 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 mutex

SYNOPSIS

 #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星星的軌跡 2024-10-10 12:39:43

查找该信息的最佳位置是 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 the pthread_mutexattr_* links in the SEE 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 (including pthreads.h).

The attributes allow you to set or get:

  • the type (deadlocking, deadlock-detecting, recursive, etc).
  • the robustness (what happens when you acquire a mutex and the original owner died while possessing it).
  • the process-shared attribute (for sharing a mutex across process boundaries).
  • the protocol (how a thread behaves in terms of priority when a higher-priority thread wants the mutex).
  • the priority ceiling (the priority at which the critical section will run, a way of preventing priority inversion).

And, for completeness, there's the init and destroy calls as well, not directly related to a specific attribute but used to create them.

赤濁 2024-10-10 12:39:43

所有互斥体属性均通过以下形式的函数在互斥体属性对象中设置:

int pthread_mutexattr_setname(pthread_attr_t *attr, Type t);

所有互斥体属性均通过以下形式的函数从互斥体属性对象中检索:

int pthread_mutexattr_getname(const pthread_attr_t *attr, Type *t);

其中名称和类型定义如下表所示:

Type and Name   Description and Value(s)
int protocol    Define the scheduling classes for mutex locks 
                PTHREAD_PRIO_NONE,PTHREAD_PRIO_PROTECT,
                PTHREAD_PRIO_INHERIT

int pshared Defines whether a mutex is shared with other processes. 
                PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE

int prioceiling Used for mutex attribute priority ceiling values. 
                See POSIX.1 section 13

int type    Application defined mutex locking
                PTHREAD_MUTEX_NORMAL,PTHREAD_MUTEX_RECURSIVE,
                PTHREAD_MUTEX_ERRORCHECK,PTHREAD_MUTEX_DEFAULT

All mutex attributes are set in a mutex attribute object by a function of the form:

int pthread_mutexattr_setname(pthread_attr_t *attr, Type t);

All mutex attributes are retrieved from a mutex attribute object by a function of the form:

int pthread_mutexattr_getname(const pthread_attr_t *attr, Type *t);

where name and Type are defined as in the table below:

Type and Name   Description and Value(s)
int protocol    Define the scheduling classes for mutex locks 
                PTHREAD_PRIO_NONE,PTHREAD_PRIO_PROTECT,
                PTHREAD_PRIO_INHERIT

int pshared Defines whether a mutex is shared with other processes. 
                PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE

int prioceiling Used for mutex attribute priority ceiling values. 
                See POSIX.1 section 13

int type    Application defined mutex locking
                PTHREAD_MUTEX_NORMAL,PTHREAD_MUTEX_RECURSIVE,
                PTHREAD_MUTEX_ERRORCHECK,PTHREAD_MUTEX_DEFAULT
贪恋 2024-10-10 12:39:43

如果向下滚动 ,你会发现一堆pthread_mutexattr_...函数,包括initdestroy和设置各种函数互斥体的属性。当您传递NULL时,将使用所有这些属性的合适默认值创建互斥锁,但如果您需要修改特定属性,您可以构造一个pthread_mutexattr_t结构并将其传入。

If you scroll down the function listing for <pthread.h>, you will find a bunch of pthread_mutexattr_... functions, including an init, destroy and functions to set various attributes of a mutex. When you pass NULL, the mutex is created with suitable defaults for all these attributes, but if you need to modify specific attributes, you can construct a pthread_mutexattr_t structure and pass it in.

雨落□心尘 2024-10-10 12:39:43

将此参数应用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文