什么是“属性"?pthread 互斥锁? [英] what is the "attribute" of a pthread mutex?

查看:21
本文介绍了什么是“属性"?pthread 互斥锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数 pthread_mutex_init 允许您指定指向属性的指针.但是我还没有找到关于 pthread 属性是什么的一个很好的解释.我一直只提供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 库功能手册
PTHREAD_MUTEX_INIT(3)

PTHREAD_MUTEX_INIT(3) BSD Library Functions Manual
PTHREAD_MUTEX_INIT(3)

名字pthread_mutex_init -- 创建一个互斥体

NAME pthread_mutex_init -- create a mutex

概要

 #include <pthread.h>

 int
 pthread_mutex_init(pthread_mutex_t *restrict mutex,
     const pthread_mutexattr_t *restrict attr);

描述pthread_mutex_init() 函数创建一个新的互斥体,具有属性指定的与属性.如果 attr 为 NULL,则使用默认属性.

DESCRIPTION The pthread_mutex_init() function creates a new mutex, with attributes specified with attr. If attr is NULL, the default attributes are used.

推荐答案

查找该信息的最佳位置是 POSIX 标准页面.

The best place to find that information is from the POSIX standards pages.

NULL 互斥属性为您提供了实现定义的默认属性.如果您想知道属性可以做什么,请查看 遵循参考并遵循 SEE ALSO 部分中的 pthread_mutexattr_* 链接.通常,默认值是一组合理的属性,但它可能因平台而异,因此我更喜欢显式创建具有已知属性的互斥锁(更好的可移植性).

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).

这是针对标准的第 7 版 1003.1-2008.起点是这里.单击左下角的 Headers 将允许您导航到特定功能(包括 pthreads.h).

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).

为了完整起见,还有 init 和 destroy 调用同样,与特定属性不直接相关,但用于创建它们.

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

这篇关于什么是“属性"?pthread 互斥锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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