views:

118

answers:

1

Are there any known problems of initialiazing pthread mutexes statically using PTHREAD_MUTEX_INITIALIZER and passing them directly for locking?

I read in some sites that this cannot be guaranteed on all platforms and also in the help page the below note is there:

Note: Mutex initialization using the PTHREAD_MUTEX_INITIALIZER does not immediately initialize the mutex. Instead, on first use, the pthread_mutex_lock() or pthread_mutex_trylock() functions branch into a slow path and cause the initialization of the mutex. Because a mutex is not just a simple memory object and requires that some resources be allocated by the system, an attempt to call pthread_mutex_destroy() or pthread_mutex_unlock() on a mutex that was statically initialized using PTHREAD_MUTEX_INITIALER and was not yet locked causes an EINVAL error.

So, if two threads call pthread_mutex_lock after static initialization, will it lead to any problem?

+1  A: 

I think it is not a problem. Given POSIX thread defines the API but not the implementation. it is not an issue if some implementations choose an some special approach. But the correct behavior of calling pthread_mutex_lock should be guaranteed.

Tristan Su