CodyChan
CodyChan

Reputation: 1865

What is the "default mutex attributes" of the pthread_mutex**

In man page for interface like pthread_mutex_init,

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

It says "If attr is NULL, the default mutex attributes are used...", and the default mutex attributes are mentioned many times, and it is also mentioned in the book The Linux Programming Interface, but it is never explained in detail anywhere, I googled and no result.

There is one post what is the "attribute" of a pthread mutex?, and it mentioned "Usually, the default is a sensible set of attributes but it may vary between platforms", but it is not what I want, I want more details.

So, what is exactly the default mutex attributes?

Upvotes: 4

Views: 3794

Answers (2)

Ulfalizer
Ulfalizer

Reputation: 4762

pthread_mutexattr_t is an opaque type (you never modify it directly) that's accessed via various pthread_mutexattr_get*()/set*() functions. Unless the documentation for those functions specify a default, then the default is up to the implementation, and you can't rely on a particular value.

You could follow the links to the various pthread_mutexattr_get*() functions in POSIX.1-2008 here and look for default values (which also apply when you pass NULL for the attributes). Here's some choice quotes:

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.

The non-type attributes are kinda obscure though.

Upvotes: 3

Trade-Ideas Philip
Trade-Ideas Philip

Reputation: 1247

The man page for pthread_mutexattr_settype gets us started. It says "The default value of the type attribute is PTHREAD_MUTEX_DEFAULT." Unfortunately it goes on to say that PTHREAD_MUTEX_DEFAULT isn't really defined and could be anything.

I wrote a short C++ program to take a closer look:

#include <pthread.h>
#include <iostream>

int main (int, char**)
{
  std::cout<<"PTHREAD_MUTEX_NORMAL = "<<PTHREAD_MUTEX_NORMAL<<std::endl
       <<"PTHREAD_MUTEX_ERRORCHECK = "<<PTHREAD_MUTEX_ERRORCHECK<<std::endl
       <<"PTHREAD_MUTEX_RECURSIVE = "<<PTHREAD_MUTEX_RECURSIVE<<std::endl
       <<"PTHREAD_MUTEX_DEFAULT = "<<PTHREAD_MUTEX_DEFAULT<<std::endl;

}

That gave me the following output:

PTHREAD_MUTEX_NORMAL = 0
PTHREAD_MUTEX_ERRORCHECK = 2
PTHREAD_MUTEX_RECURSIVE = 1
PTHREAD_MUTEX_DEFAULT = 0

So at least on my system the default is PTHREAD_MUTEX_NORMAL, i.e. "fast" / non-recursive.

Upvotes: 2

Related Questions