Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are pthread attribute objects required to exist for the lifetime of the object that uses them?

Tags:

posix

pthreads

Are pthread attribute objects required to exist for the lifetime of the object that uses them, or is it safe to destroy them immediately after they've been used? For example:

// Create the mutex attributes.
pthread_mutexattr_t attributes;
pthread_mutexattr_init( &attributes );
pthread_mutexattr_settype( &attributes, PTHREAD_MUTEX_NORMAL );

// Create the mutex using the attributes from above.
pthread_mutex_t mutex;
pthread_mutex_init( &mutex, &attributes );

Can the attributes now be safely destroyed with pthread_mutexattr_destroy(), or is it required to wait until after the mutex has been destroyed with pthread_mutex_destroy()?

Does the same apply for the other pthread objects that use attributes?

like image 859
x-x Avatar asked Jun 16 '12 09:06

x-x


1 Answers

http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutexattr_init.html

After a mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes object (including destruction) shall not affect any previously initialized mutexes.

So it's perfectly safe to destroy the mutexattr object after you're done creating your mutexes.

like image 144
bdonlan Avatar answered Oct 17 '22 04:10

bdonlan