Do i need to call pthread_mutex_destroy on a mutex?

I use pthread_mutex_t in a C ++ program, as shown below:

class Mutex : public noncopyable { public: Mutex() { pthread_mutex_init(&m_mutex, NULL); } void acquire() { pthread_mutex_lock(&m_mutex); } void release() { pthread_mutex_unlock(&m_mutex); } private: pthread_mutex_t m_mutex; }; 

(The class is not copied - http://www.boost.org/doc/libs/1_53_0/boost/noncopyable.hpp )

What I do not understand is considered a mistake in order not to call pthread_mutex_destroy in the destructor? The documentation I read does not indicate that destroy should be called.

Does anyone know what pthread_mutex_destroy actually does and under what conditions is required?

EDIT

Is the answer to pthread_mutex_destroy also applicable to pthread_cond_destroy etc.? They seem to me almost useless functions, if only pthread_mutex_init et. and others allocate memory? (the documents, for me, are not entirely clear on this.)

I can not harm the so-called destruction, so the question is mostly academic.

On linux, in any case, it seems that destruction only sets the mutex to an invalid state:

 int __pthread_mutex_destroy (mutex) pthread_mutex_t *mutex; { if ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) == 0 && mutex->__data.__nusers != 0) return EBUSY; /* Set to an invalid value. */ mutex->__data.__kind = -1; return 0; } 

(From glibc-2.14 / nptl / pthread_mutex_destroy.c).

+8
c ++ pthreads posix mutex
source share
3 answers

If someone provides you with the destroy function, then you need to call it the final action on this object before it goes out of scope.

In architecture and implementations where the API has no effect, this will be optimized, however, if the API changes in the future to require clearing the internal state, and your code does not call it, your code will now have a memory and / or resource leak.

So the simple answer is yes; you have to call this API - and this is - even if the API does nothing at the moment, because although the API itself is fixed forever in the future, there is no API implementation.

+13
source share

From the IEEE documentation , which is the standard POSIX manager:

The pthread_mutex_destroy () function must destroy the mutex object referenced by mutex; the mutex object becomes essentially uninitialized. An implementation may force pthread_mutex_destroy () to set the object referenced by mutex to an invalid value. A destroyed mutex object can be reinitialized with pthread_mutex_init (); the results that reference the object after its destruction are undefined.

The documentation does not state that you should call it. But this is a good practice. A call to this api will signal the POSIX library to release all the resources that were reserved for use of this particular mutex object during its initialization.
It is logical to assume that mutex initialization allocates / reserves some resources.

+5
source share

Several years passed, and @SecurityMatt was right. To settle the discussion, you must call pthread_mutex_destroy to satisfy the API requirements and potentially free up memory.

Here is an excerpt from the last pthread_mutex_destroy :

 int _pthread_mutex_destroy (pthread_mutex_t *mutex) { if (mutex->__attr == __PTHREAD_ERRORCHECK_MUTEXATTR || mutex->__attr == __PTHREAD_RECURSIVE_MUTEXATTR) /* Static attributes. */ ; else free (mutex->__attr); return 0; } 
0
source share

All Articles