Linux – What are the “default mutex attributes” of pthread_mutex**

What are the “default mutex attributes” of pthread_mutex**… here is a solution to the problem.

What are the “default mutex attributes” of pthread_mutex**

In the man pages for interfaces such as pthread_mutex_init

int pthread_mutex_init(pthread_mutex_t *restrict mutex,
          const pthread_mutexattr_t *restrict attr);

It says “If attr is NULL, use the default mutex attribute…”, the default mutex attribute is mentioned several times, it is also mentioned in The Linux Programming Interface book, but never explained in detail anywhere, I googled it without results.

There is a

post what is the “attribute” of a pthread mutex? , which mentions “usually, the default is a reasonable set of properties, but it may vary from platform to platform”, but that’s not what I want, I want more details.

So, what exactly are the default mutex properties?

Solution

pthread_mutexattr_t is an opaque type (you never modify it directly) and is accessible through various pthread_mutexattr_get*()/set*() functions. Unless the documentation for those functions specifies a default value, the default value depends on the implementation, and you cannot rely on a specific value.

You can click POSIX.1-2008 various pthread_mutexattr_get*() here Links to functions. and look for the default value (also applies when you pass NULL for the property). Here are some selection offers:

pthread_mutexattr_getprotocol() :

The default value of the attribute shall be PTHREAD_PRIO_NONE.

pthread_mutexattr_gettype() :

The default value of the type attribute is PTHREAD_MUTEX_DEFAULT.

pthread_mutexattr_getpshared() :

The default value of the attribute shall be PTHREAD_PROCESS_PRIVATE.

pthread_mutexattr_getrobust() :

PTHREAD_MUTEX_STALLED … This is the default value.

Although the non-type property is a bit obscure.

Related Problems and Solutions