pthread_mutexattr_init() — Initialize a mutex attribute object
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a |
both | POSIX(ON) |
Format
#define _OPEN_THREADS
#define _OPEN_SYS
#include <pthread.h>
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
SUSV3:
#define _UNIX03_THREADS
#define _OPEN_SYS
#include <pthread.h>
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
General description
Initializes a mutex attribute object. With a mutex attribute object, you can manage the characteristics of mutexes in your application. 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 defining those characteristics for each and every mutex.
For a valid mutex attribute, refer to pthread_mutexattr_setkind_np() — Set kind for a mutex attribute object.
Note: Before freeing up the
storage containing the pthread_mutexattr_t object, be sure to destroy
it by calling pthread_mutexattr_destroy(). If the pthread_mutexattr_t
object is not destroyed before the storage is reused, the results
are undefined.
Returned value
If successful, pthread_mutexattr_init() returns 0.
If unsuccessful, pthread_mutexattr_init() returns
-1 and sets errno to one of the following values:
- Error Code
- Description
- ENOMEM
- There is not enough memory to initialize attr.
Special behavior for Single UNIX Specification, Version 3:
If unsuccessful, pthread_mutexattr_init() returns an error number to indicate the error.
Example
CELEBP44
/* CELEBP44 */
#define _OPEN_THREADS
#define _OPEN_SYS /* Needed to identify __MUTEX_RECURSIVE */
#include <pthread.h>
#include <stdio.h>
main() {
pthread_mutexattr_t attr;
pthread_mutex_t mutex;
if (pthread_mutexattr_init(&attr) != 0) {
perror("pthread_mutex_attr_init() error");
exit(1);
}
if (pthread_mutexattr_setkind_np(&attr, __MUTEX_RECURSIVE) != 0) {
perror("pthread_mutex_attr_setkind_np() error");
exit(2);
}
if (pthread_mutex_init(&mutex, &attr) != 0) {
perror("pthread_mutex_init() error");
exit(3);
}
if (pthread_mutexattr_destroy(&attr) != 0) {
perror("pthread_mutex_attr_destroy() error");
exit(4);
}
}