3

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?

alk
  • 69,737
  • 10
  • 105
  • 255
olegst
  • 1,209
  • 1
  • 13
  • 33

1 Answers1

8

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

lulijeta
  • 900
  • 1
  • 9
  • 19