Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

what is the "attribute" of a pthread mutex?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...