Suppose that I have a mutex/condition variable. I initialize (pthread_mutex_init/pthread_cond_init), but never use them (no lock operations, no signal/wait).
Do these unused objects allocate other resources other than the memory they occupy?
(I'm interested in linux and OSX behavior in this regard)
Although POSIX explicitly contemplates that these functions may fail due to a lack of resources (the EAGAIN
and ENOMEM
error codes), neither the Linux glibc nor OSX implementations of pthread_mutex_init()
or pthread_cond_init()
allocate any resources. You can see this by direct inspection of the source code:
The Linux glibc implementation of pthread_mutex_init()
is in nptl/pthread_mutex_init.c
(see the function __pthread_mutex_init()
).
The Linux glibc implementation of pthread_cond_init()
is in nptl/pthread_cond_init.c
(see the function __pthread_cond_init()
).
The OSX implementation of pthread_mutex_init()
is in libpthread/pthread_mutex.c
(see the functions pthread_mutex_init()
and _pthread_mutex_init()
) and libpthread/internal.h
(see the macro _PTHREAD_LOCK_INIT
).
The OSX implementation of pthread_cond_init()
is in libpthread/pthread_cond.c
(see the functions pthread_cond_init()
and _pthread_cond_init()
).