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 <pthread.h> #define NUM_THREADS 5 /*initialize static mutex array*/ static pthread_mutex_t mutexes[NUM_THREADS] = ...?
Or must they be allocated dynamically?
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.