|
Syntax #include <pthread.h> int pthread_mutexattr_init(pthread_mutexattr_t *attr); Threadsafe: Yes Signal Safe: Yes |
The pthread_mutexattr_init() function initializes the mutex attributes object referenced by attr to the default attributes. The mutex attributes object can be used in a call to pthread_mutex_init() to create a mutex.
| Attribute | Default value | supported values |
|---|---|---|
| pshared | PTHREAD_PROCESS_PRIVATE | PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED |
| kind (non portable) | PTHREAD_MUTEX_NONRECURSIVE_NP | PTHREAD_MUTEX_NONRECURSIVE_NP or PTHREAD_MUTEX_RECURSIVE_NP |
| name (non portable) | PTHREAD_DEFAULT_MUTEX_NAME_NP "QP0WMTX UNNAMED" | Any name 15 characters or less. If not terminated by a null character, name is truncated to 15 characters. |
| type | PTHREAD_MUTEX_DEFAULT (PTHREAD_MUTEX_NORMAL) | PTHREAD_MUTEX_DEFAULT or PTHREAD_MUTEX_NORMAL or PTHREAD_MUTEX_RECURSIVE or PTHREAD_MUTEX_ERRORCHECK or PTHREAD_MUTEX_OWNERTERM_NP The PTHREAD_MUTEX_OWNERTERM_NP attribute value is non portable |
None.
If pthread_mutexattr_init() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.
#include <pthread.h>
#include <stdio.h>
#include "check.h"
pthread_mutex_t mutex;
int main(int argc, char **argv)
{
int rc=0;
pthread_mutexattr_t mta;
printf("Entering testcase\n");
printf("Create a default mutex attribute\n");
rc = pthread_mutexattr_init(&mta);
checkResults("pthread_mutexattr_init\n", rc);
printf("Create the mutex using a mutex attributes object\n");
rc = pthread_mutex_init(&mutex, &mta);
checkResults("pthread_mutex_init(mta)\n", rc);
printf("- At this point, the mutex with its default attributes\n");
printf("- Can be used from any threads that want to use it\n");
printf("Destroy mutex attribute\n");
rc = pthread_mutexattr_destroy(&mta);
checkResults("pthread_mutexattr_destroy()\n", rc);
printf("Destroy mutex\n");
rc = pthread_mutex_destroy(&mutex);
checkResults("pthread_mutex_destroy()\n", rc);
printf("Main completed\n");
return 0;
}
Output
Entering testcase Create a default mutex attribute Create the mutex using a mutex attributes object - At this point, the mutex with its default attributes - Can be used from any threads that want to use it Destroy mutex attribute Destroy mutex Main completed