pthread_mutexattr_getkind_np() — Get kind from a mutex attribute object

Standards

Standards / Extensions C or C++ Dependencies
z/OS® UNIX both

POSIX(ON)

Format

#define _OPEN_THREADS
#define _OPEN_SYS
#include <pthread.h>

int pthread_mutexattr_getkind_np(pthread_mutexattr_t *attr, int *kind);

General description

Gets the attribute kind from the mutex attribute object attr. 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.

The values for the attribute kind are:
__MUTEX_NONRECURSIVE
A nonrecursive mutex can be locked only once. That is, the mutex is either in a locked or unlocked state for a thread. If a thread attempts to lock a mutex that it has already locked, an error is returned.
_MUTEX_RECURSIVE
A recursive mutex can be locked more than once by the same thread. A count of the number of times the mutex has been locked is maintained. The mutex is unlocked when pthread_mutex_unlock() is performed an equal number of times.
__MUTEX_NONRECURSIVE + __MUTEX_NODEBUG
A nonrecursive mutex can be given an additional attribute, NODEBUG. This indicates that state changes to this mutex will not be reported to the debug interface, even if present.
__MUTEX_RECURSIVE + __MUTEX_NODEBUG
A recursive mutex can be given an additional attribute, NODEBUG. This indicates that state changes to this mutex will not be reported to the debug interface, even if present.

Returned value

If successful, pthread_mutexattr_getkind_np() returns 0.

If unsuccessful, pthread_mutexattr_getkind_np() returns -1 and sets errno to one of the following values:
Error Code
Description
EINVAL
The value specified for attr is not valid.

Example

CELEBP43
⁄* CELEBP43 *⁄

#pragma runopts(TEST(ALL))

#ifndef _OPEN_THREADS
#define _OPEN_THREADS
#define _OPEN_SYS     ⁄* Needed to identify __MUTEX_RECURSIVE,
                         __MUTEX_NODEBUG, and __MUTEX_NONRECURSIVE  *⁄
#endif

#include <stdio.h>
#include <pthread.h>

pthread_mutexattr_t attr;

int kind;

main() {
    if (pthread_mutexattr_init(&attr) == -1) {
       perror("pthread_mutexattr_init()");
       exit(1);
     }

    if (pthread_mutexattr_setkind_np(&attr, \
               __MUTEX_RECURSIVE + __MUTEX_NODEBUG) == -1 ) {

       perror("pthread_mutexattr_setkind_np()");
       exit(1);
     }

    if (pthread_mutexattr_getkind_np(&attr, &kind) == -1) {
       perror("pthread_mutexattr_getkind_np()");
       exit(1);
     }

     switch(kind) {

       case __MUTEX_NONRECURSIVE:
         printf("\nmutex will be nonrecursive");
         break;

       case __MUTEX_NONRECURSIVE+__MUTEX_NODEBUG:
         printf("\nmutex will be nonrecursive + nodebug");
         break;

       case __MUTEX_RECURSIVE:
         printf("\nmutex will be recursive");
         break;

       case __MUTEX_RECURSIVE+__MUTEX_NODEBUG:
         printf("\nmutex will be recursive + nodebug");
         break;

       default:
         printf("\nattribute kind value returned by \
 pthread_mutexattr_getkind_np() unrecognized");
         exit(1);
     }
    exit(0);
}
Output:
a default mutex will be nonrecursive

Related information