Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a |
both | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
#define _UNIX03_THREADS
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t * __restrict__mutex,
const pthread_mutexattr_t * __restrict__attr);
Creates a mutex, referenced by mutex, with attributes specified by attr. If attr is NULL, the default mutex attribute (NONRECURSIVE) is used.
If successful, pthread_mutex_init() returns 0, and the state of the mutex becomes initialized and unlocked.
Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_mutex_init() returns an error number to indicate the error.
The _OPEN_SYS_MUTEX_EXT feature switch can be optionally included. If the feature is set, then significantly larger pthread_mutex_t objects will be defined. The feature is used for the management of mutex and condition variables in shared memory.
If the supplied extended pthread_mutex_t object is not in shared memory, pthread_mutex_init() will treat the object as a non-shared object, since it is not accessible to any other process.
It is recommended that you define and initialize the pthread_mutex_t objects in the same compile unit. If you pass a pthread_mutex_t object around to be initialized, make sure the initialization code has been compiled with the same _OPEN_SYS_MUTEX_EXT feature setting as the code that defines the object.
⁄* CELEBP37 *⁄
#ifndef _OPEN_THREADS
#define _OPEN_THREADS
#endif
#include <pthread.h>
main() {
pthread_mutexattr_t attr;
pthread_mutex_t mut;
if (pthread_mutexattr_init(&attr) == -1) {
perror("mutexattr_init error");
exit(1);
}
if (pthread_mutex_init(&mut, &attr) == -1) {
perror("mutex_init error");
exit(2);
}
exit(0);
}