pthread_mutexattr_init()--Initialize Mutex Attributes Object
Syntax:
#include <pthread.h> int pthread_mutexattr_init(pthread_mutexattr_t *attr);Service Program Name: QP0WPTHR
Default Public Authority: *USE
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.
Authorities and Locks
None.
Parameters
- attr
- (Input/Output) Address of the variable to contain the mutex attributes object
Return Value
- 0
- pthread_mutexattr_init() was successful.
- value
- pthread_mutexattr_init() was not successful. value is set to indicate the error condition.
Error Conditions
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.
- [EINVAL]
The value specified for the argument is not correct.
Related Information
- The <pthread.h> header file. See Header files for Pthread functions.
- pthread_mutexattr_destroy()--Destroy Mutex Attributes Object
- pthread_mutex_destroy()--Destroy Mutex
- pthread_mutex_init()--Initialize Mutex
Example
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
#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
API introduced: V4R3
[ Back to top | Pthread APIs | APIs by category ]