Is it possible to destroy a mutexattr object before using a mutex that was initialized with this mutexattr?

Consider the following code:

pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); pthread_mutex_t mut; pthread_mutex_init(&mut, &attr); pthread_mutexattr_destroy(&attr); pthread_mutex_lock(&mut); 

Is this code valid?

If the mutex is allowed to contain references to the attribute object with which it was initialized, then I believe that I cannot call pthread_mutexattr_destroy(&attr) before using the mutex.

+5
source share
1 answer

As a guide :

After the mutex attribute has been used to initialize one or more mutexes, any function that affects the attribute object (including destruction) should not affect previously initialized mutexes.

So, your code is valid (but don't forget to check the potential error of these functions).

+6
source

Source: https://habr.com/ru/post/1268522/


All Articles