---
name: "pthread_mutexattr_init"
section: "3thr"
platform: "linux"
package: "glibc-doc"
version: "2.1.3-20"
release: "Debian 2.2 (Potato)"
platforms: ["linux"]
url: "https://new.carta.tech/pthread_mutexattr_init/3thr"
---

# pthread\_mutexattr\_init(3thr)

mutex creation attributes

## NAME

pthread\_mutexattr\_init, pthread\_mutexattr\_destroy, pthread\_mutexattr\_setkind\_np, pthread\_mutexattr\_getkind\_np - mutex creation attributes

## SYNOPSIS

**#include \<pthread.h\>**

**int pthread\_mutexattr\_init(pthread\_mutexattr\_t \****attr***);**

**int pthread\_mutexattr\_destroy(pthread\_mutexattr\_t \****attr***);**

**int pthread\_mutexattr\_setkind\_np(pthread\_mutexattr\_t \****attr***, int** *kind***);**

**int pthread\_mutexattr\_getkind\_np(const pthread\_mutexattr\_t \****attr***, int \****kind***);**

## DESCRIPTION

Mutex attributes can be specified at mutex creation time, by passing a mutex attribute object as second argument to  [**pthread\_mutex\_init**(3)](/pthread_mutex_init/3.md). Passing  **NULL** is equivalent to passing a mutex attribute object with all attributes set to their default values.

**pthread\_mutexattr\_init** initializes the mutex attribute object  *attr* and fills it with default values for the attributes.

**pthread\_mutexattr\_destroy** destroys a mutex attribute object, which must not be reused until it is reinitialized.  **pthread\_mutexattr\_destroy** does nothing in the LinuxThreads implementation.

LinuxThreads supports only one mutex attribute: the mutex kind, which is either  **PTHREAD\_MUTEX\_FAST\_NP** for \`\`fast'' mutexes, **PTHREAD\_MUTEX\_RECURSIVE\_NP** for \`\`recursive'' mutexes, or  **PTHREAD\_MUTEX\_ERRORCHECK\_NP** for \`\`error checking'' mutexes. As the  **NP** suffix indicates, this is a non-portable extension to the POSIX standard and should not be employed in portable programs.

The mutex kind determines what happens if a thread attempts to lock a mutex it already owns with  [**pthread\_mutex\_lock**(3)](/pthread_mutex_lock/3.md). If the mutex is of the \`\`fast'' kind,  [**pthread\_mutex\_lock**(3)](/pthread_mutex_lock/3.md) simply suspends the calling thread forever.  If the mutex is of the \`\`error checking'' kind, [**pthread\_mutex\_lock**(3)](/pthread_mutex_lock/3.md) returns immediately with the error code **EDEADLK**. If the mutex is of the \`\`recursive'' kind, the call to [**pthread\_mutex\_lock**(3)](/pthread_mutex_lock/3.md) returns immediately with a success return code. The number of times the thread owning the mutex has locked it is recorded in the mutex. The owning thread must call [**pthread\_mutex\_unlock**(3)](/pthread_mutex_unlock/3.md) the same number of times before the mutex returns to the unlocked state.

The default mutex kind is \`\`fast'', that is,  **PTHREAD\_MUTEX\_FAST\_NP**.

**pthread\_mutexattr\_setkind\_np** sets the mutex kind attribute in  *attr* to the value specified by  *kind*.

**pthread\_mutexattr\_getkind\_np** retrieves the current value of the mutex kind attribute in  *attr* and stores it in the location pointed to by  *kind*.

## RETURN VALUE

**pthread\_mutexattr\_init**, **pthread\_mutexattr\_destroy** and **pthread\_mutexattr\_getkind\_np** always return 0.

**pthread\_mutexattr\_setkind\_np** returns 0 on success and a non-zero error code on error.

## ERRORS

On error,  **pthread\_mutexattr\_setkind\_np** returns the following error code:

- **EINVAL**

  *kind* is neither  **PTHREAD\_MUTEX\_FAST\_NP** nor  **PTHREAD\_MUTEX\_RECURSIVE\_NP** nor  **PTHREAD\_MUTEX\_ERRORCHECK\_NP**

## AUTHOR

Xavier Leroy \<Xavier.Leroy@inria.fr\>

## SEE ALSO

[**pthread\_mutex\_init**(3)](/pthread_mutex_init/3.md), [**pthread\_mutex\_lock**(3)](/pthread_mutex_lock/3.md), [**pthread\_mutex\_unlock**(3)](/pthread_mutex_unlock/3.md).
