I've never had the chance to play with the pthreads library before, but I am reviewing some code involving pthread mutexes. I checked the documentation for pthread_mutex_lock
and pthread_mutex_init
, and my understanding from reading the man pages for both these functions is that I must call pthread_mutex_init
before I call pthread_mutex_lock
.
However, I asked a couple colleagues, and they think it is okay to call pthread_mutex_lock
before calling pthread_mutex_init
. The code I'm reviewing also calls pthread_mutex_lock
without even calling pthread_mutex_init
.
Basically, is it safe and smart to call pthread_mutex_lock
before ever calling pthread_mutex_init
(if pthread_mutex_init
even gets called)?
EDIT: I also see some examples where pthread_mutex_lock
is called when pthread_mutex_init
is not used, such as this example
EDIT #2: Here is specifically the code I'm reviewing. Please note that the configure function acquires and attaches to some shared memory that does not get initialized. The Java code later on will call lock()
, with no other native functions called in-between. Link to code