18

Using pthreads, how would one, in C, initialize a static array of mutexes?

For a single static mutex, it seems I can use PTHREAD_MUTEX_INITIALIZER. But what about an static array of them? As, in for example,

#include &ltpthread.h&gt
#define NUM_THREADS 5

/*initialize static mutex array*/
static pthread_mutex_t mutexes[NUM_THREADS] = ...?

Or must they be allocated dynamically?

ManRow
  • 1,473
  • 4
  • 21
  • 39

2 Answers2

25

No, you don't have to create them dynamically. You can use a static array, you just have to get them all set up before you use them. You can do:

#define NUM_THREADS 5
static pthread_mutex_t mutexes[NUM_THREADS] = {
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER
};

which is prone to errors if you ever change NUM_THREADS, though that can be fixed with something like:

static pthread_mutex_t mutexes[] = {
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_INITIALIZER
};
#define NUM_THREADS (sizeof(mutexes)/sizeof(*mutexes))

Alternatively, you can do it with code, such as:

#define NUM_THREADS 5
static pthread_mutex_t mutexes[NUM_THREADS];

// Other stuff

int main (void) {
    for (int i = 0; i < NUM_THREADS; i++)
        pthread_mutex_init(&mutexes[i], NULL);
    // Now you can use them safely.

    return 0;
}

In all those cases, they're correctly set up before you try to use them. In fact, I'd do it well before you do any threading stuff but that's just me being paranoid.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • In the first sample the use of `NUM_THREADS` is not necessary. It may help you to catch certain errors, but also to commit certain other ones. – rlibby Feb 28 '11 at 06:30
  • you could also use BOOST_PP_ENUM since it only requires c99 preprocessor – Anycorn Feb 28 '11 at 06:32
17

If you have a C99 conforming compiler you can use P99 to do your initialization:

static pthread_mutex_t mutexes[NUM_THREADS] =
  { P99_DUPL(NUM_THREADS, PTHREAD_MUTEX_INITIALIZER) };

This just repeats the token sequence PTHREAD_MUTEX_INITIALIZER, the requested number of times.

For this to work you only have to be sure that NUM_THREADS doesn't expand to a variable but to a decimal integer constant that is visible to the preprocessor and that is not too large.

Jens Gustedt
  • 72,200
  • 3
  • 92
  • 164