BayE
BayE

Reputation: 75

Pthread: when Dynamic mutex initialization must be used

As everyone knows, there are two ways how to init pthread mutex (C language)

  1. Static initialization:

    pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
    
  2. Dynamic initialization:

    pthread_mutexattr_t attr;
    pthread_mutex_t mutex;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP); 
    pthread_mutex_init(&mutex, &attr);
    

var 1 - I used it many times.

var 2 - the reason I posted this question: Will really appreciate if someone could provide me with some real example(s) when dynamic initialization of pthread mutex must be used.

Thanks!

Upvotes: 1

Views: 216

Answers (1)

alk
alk

Reputation: 70981

You always need the pthread_*_init() functions if the default attributes won't do/are not suitable.

An example can be found at the bottom of this page.

Upvotes: 2

Related Questions