2

Can I create and use only one mutex attribute to initialize multiple recursive mutexes? Or do I have to create one mutex attribute for each mutex I want to create? Basically I the following code correct?

int err;
int bufferLength = 10;
pthread_mutexattr_t recursiveAttr;
pthread_mutex_t mutexes[bufferLength];

for(int index = 0; index < bufferLength; index++){
    err = pthread_mutex_init(&mutexes[i], &recursiveAttr);
    if(err != 0){
        perror("Error initializing the mutex");
    }
}
Daniel Oliveira
  • 1,280
  • 14
  • 36

1 Answers1

2

You can use the same attribute object for multiple mutexes.

Note however, that the pthread_mutexattr_t object you're using must be initialized itself. To initialize a pthread_mutexattr_t you must use pthread_mutexattr_init (and eventually, pthread_mutexattr_destroy), both of which should be done once. Your current code makes no such calls, and should do so to be compliant.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141