Let's suppose we have a global mutex or rwlock initialized with a static initializer:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
or
pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
Documentation says it's equivalent to pthread_*_init with default parameters.
Do we have to destroy a mutex or rwlock initialized this way?
No. The difference between a statically allocated and a dynamically allocated mutex is basically comparable to a variable located on the stack or in the heap. You don't have to give a mutex back that you didn't allocate dynamically. Quoting from Michael Kerrisk's "The Linux Programming Interface":
When an automatically or dynamically allocated mutex is no longer required, it should be destroyed using pthread_mutex_destroy(). (It is not necessary to call pthread_mutex_destroy() on a mutex that was statically initialized using PTHREAD_MUTEX_INITIALIZER.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With