Reputation: 1043
I have a couple of questions regarding pthread attributes that I could not find answers to elsewhere.
Upvotes: 4
Views: 1509
Reputation: 754810
If you read the POSIX specification for pthread_mutexattr_init()
, it says:
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.
Similarly, though not quite so clearly, the specification for pthread_attr_init()
says:
The resulting attributes object (possibly modified by setting individual attribute values) when used by
pthread_create()
defines the attributes of the thread created. A single attributes object can be used in multiple simultaneous calls topthread_create()
.
And the specification of pthread_create()
says:
The
pthread_create()
function shall create a new thread, with attributes specified byattr
, within a process. Ifattr
is NULL, the default attributes shall be used. If the attributes specified byattr
are modified later, the thread's attributes shall not be affected.
I think those quotes mean that the answers are:
Yes, you may destroy the attribute object when it is convenient. In effect, the POSIX calls make a copy of the attributes.
Yes, it is safe to reuse attributes.
Upvotes: 4