105

Is there any difference between

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

Or

pthread_mutex_t lock;
pthread_mutex_init ( &lock, NULL);

Am I safe enough if I use only the first method ?

NOTE: My question mostly refers to very small programs where at the most what I'll do is connect several clients to a server and resolve their inquiries with worker threads.

Kalec
  • 2,681
  • 9
  • 30
  • 49

4 Answers4

85

By older versions of the POSIX standard the first method with an initializer is only guaranteed to work with statically allocated variables, not when the variable is an auto variable that is defined in a function body. Although I have never seen a platform where this would not be allowed, even for auto variables, and this restriction has been removed in the latest version of the POSIX standard.

The static variant is really preferable if you may, since it allows to write bootstrap code much easier. Whenever at run time you enter into code that uses such a mutex, you can be assured that the mutex is initialized. This is a precious information in multi-threading context.

The method using an init function is preferable when you need special properties for your mutex, such as being recursive e.g or being shareable between processes, not only between threads.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
14

I would like to quote this from this book:

With POSIX threads, there are two ways to initialize locks. One way to do this is to use PTHREAD_MUTEX_INITIALIZER, as follows: pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

Doing so sets the lock to the default values and thus makes the lock usable. The dynamic way to do it (i.e., at run time) is to make a call to pthread_mutex_init() as follows: int rc = pthread_mutex_init(&lock, NULL); assert(rc == 0); // always check success!

The first argument to this routine is the address of the lock itself, whereas the second is an optional set of attributes. Read more about the attributes yourself; passing NULL in simply uses the defaults. Either way works, but we usually use the dynamic (latter) method.

Namoshek
  • 6,394
  • 2
  • 19
  • 31
Mari202
  • 141
  • 1
  • 5
8

You can set more attributes of the mutex with the dynamic initialisation, plus you can only use the dynamic method if you're adding a bunch of mutexes at run time.

There's nothing wrong with the static approach though, if that fits your needs.

Joe
  • 7,378
  • 4
  • 37
  • 54
  • " _plus you can only use the dynamic method if you're adding a bunch of mutexes at run time._ " So, what does this mean ? A small example if it's not easy to explain ? – Kalec Jan 14 '13 at 14:24
  • 1
    @Kalec: if your mutex is allocated by `malloc()` (or belongs to an object that is allocated). – Michael Burr Jan 17 '13 at 22:00
  • 3
    @Kalec if mutex variable "lock" is part of a structure , then we can not follow 1st approach. we have to use pthread_init(). – pankaj kushwaha Sep 12 '15 at 18:25
7

In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes.

If you want to specify attributes for mutex go with dynamic initialization ........

The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attrspecified as NULL, except that no error checks are performed.