Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy static mutex and rwlock initializers

Tags:

c

posix

pthreads

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?

like image 789
olegst Avatar asked Apr 28 '26 08:04

olegst


1 Answers

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.)

like image 70
lulijeta Avatar answered Apr 30 '26 21:04

lulijeta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!