stack allocation of pthread_mutex_t with initializer

Godmar Back godmar@gmail.com
Fri Aug 9 17:19:00 GMT 2013


Hi,

I have a question about the semantics of PTHREAD_MUTEX_INITIALIZER/
pthread_mutex_destroy.

Is it required to call pthread_mutex_destroy on mutexes allocated as
automatic variables?
In other words, could the following code create a memory leak, in GNU
Libc or possibly in other conforming POSIX implementations?

void function_called_many_times() {
   pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER;

    // create threads, do work, use M
    // M goes out of scope
}

Some online sources claim that PTHREAD_MUTEX_INITIALIZER can't be used
for automatic variables, but I've been unable to find an authoritative
source for that statement.

The POSIX standard explicitly wants to keep the mutex type opaque to
allow for both inlined & out of object implementations, but doesn't
address reclamation for mutexes declared using the static initializer
syntax.

 - Godmar



More information about the Libc-help mailing list