In older versions of the POSIX standard, the first method with an initializer is guaranteed only for working with statically assigned variables, and not when the variable is an auto variable that is defined in the function body. Although I have never seen a platform where this would be prohibited, even for auto variables, and this restriction has been removed in the latest version of the POSIX standard.
The static option is really preferable if you can, since it makes it easier to write bootstrap code. Whenever at runtime you enter code that uses such a mutex, you can be sure that the mutex is initialized. This is valuable information in a multi-threaded context.
A method using the init function is preferred when you need special properties for your mutex, such as recursive ones, for example, or sharing between processes, not just between threads.
Jens Gustedt Jan 14 '13 at 14:44 2013-01-14 14:44
source share