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.
The mutex attributes object is an abstract object, containing several attributes, depending on the implementation of POSIX options. It is accessed through a variable of type pthread_mutexattr_t. In AIX®, the pthread_mutexattr_t data type is a pointer; on other systems, it may be a structure or another data type.
It defines the set of values to be used for the mutex during its creation. By establishing a mutex attribute object, you can create many mutexes with the same set of characteristics, without needing to define the characteristics for each and every mutex. The values for the attribute type are: PTHREAD_MUTEX_NORMAL.
Mutex lock will only be released by the thread who locked it. So this ensures that once a thread has locked a piece of code then no other thread can execute the same region until it is unlocked by the thread who locked it.
The method using an init function is preferable when you need special properties for your mutex, such as being recursive e.g or being shareable between processes, not only between threads. Show activity on this post. Doing so sets the lock to the default values and thus makes the lock usable.
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:
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With