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