直接上英文解释:
pthread_mutex_init()如下:
NAME
pthread_mutex_init, pthread_mutex_destroy - initialise or destroy a mutex
SYNOPSIS
#include <pthread.h> int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
DESCRIPTION
The pthread_mutex_init() function initialises the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect is the same as passing the address of a default mutex attributes object. Upon successful initialisation, the state of the mutex becomes initialised and unlocked.
Attempting to initialise an already initialised mutex results in undefined behaviour.
The pthread_mutex_destroy() function destroys the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialised. An implementation may cause pthread_mutex_destroy() to set the object referenced by mutexto an invalid value. A destroyed mutex object can be re-initialised using pthread_mutex_init(); the results of otherwise referencing the object after it has been destroyed are undefined.
It is safe to destroy an initialised mutex that is unlocked. Attempting to destroy a locked mutex results in undefined behaviour.
In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialise mutexes that are statically allocated. The effect is equivalent to dynamic initialisation by a call topthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.
RETURN VALUE
If successful, the pthread_mutex_init() and pthread_mutex_destroy() functions return zero. Otherwise, an error number is returned to indicate the error. The [EBUSY] and [EINVAL] error checks, if implemented, act as if they were performed immediately at the beginning of processing for the function and cause an error return prior to modifying the state of the mutex specified by mutex.
ERRORS
The pthread_mutex_init() function will fail if:
- [EAGAIN]
- The system lacked the necessary resources (other than memory) to initialise another mutex.
- [ENOMEM]
- Insufficient memory exists to initialise the mutex.
- [EPERM]
- The caller does not have the privilege to perform the operation.
The pthread_mutex_init() function may fail if:
- [EBUSY]
- The implementation has detected an attempt to re-initialise the object referenced by mutex, a previously initialised, but not yet destroyed, mutex.
- [EINVAL]
- The value specified by attr is invalid.
The pthread_mutex_destroy() function may fail if:
- [EBUSY]
- The implementation has detected an attempt to destroy the object referenced by mutex while it is locked or referenced (for example, while being used in a pthread_cond_wait() or pthread_cond_timedwait()) by another thread.
- [EINVAL]
- The value specified by mutex is invalid.
These functions will not return an error code of [EINTR].
pthread_mutexattr_init()如下:
NAME
pthread_mutexattr_init, pthread_mutexattr_destroy - initialise and destroy mutex attributes object
SYNOPSIS
#include <pthread.h> int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);DESCRIPTION
The function pthread_mutexattr_init() initialises a mutex attributes object attr with the default value for all of the attributes defined by the implementation.
The effect of initialising an already initialised mutex attributes object is undefined.
After a mutex attributes object has been used to initialise one or more mutexes, any function affecting the attributes object (including destruction) does not affect any previously initialised mutexes.
The pthread_mutexattr_destroy() function destroys a mutex attributes object; the object becomes, in effect, uninitialised. An implementation may cause pthread_mutexattr_destroy() to set the object referenced by attr to an invalid value. A destroyed mutex attributes object can be re-initialised using pthread_mutexattr_init(); the results of otherwise referencing the object after it has been destroyed are undefined.
RETURN VALUE
Upon successful completion, pthread_mutexattr_init() and pthread_mutexattr_destroy() return zero. Otherwise, an error number is returned to indicate the error.
ERRORS
The pthread_mutexattr_init() function may fail if:
- [ENOMEM]
- Insufficient memory exists to initialise the mutex attributes object.
The pthread_mutexattr_destroy() function may fail if:
- [EINVAL]
- The value specified by attr is invalid.
These functions will not return an error code of [EINTR].