Reputation: 1290
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");
}
}
Upvotes: 3
Views: 480
Reputation: 66194
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.
Upvotes: 3