pthread属性对象是否需要在使用它们的对象的生命周期中存在?

时间:2012-06-16 09:32:23

标签: pthreads posix

pthread属性对象是否需要在使用它们的对象的生命周期内存在,或者在使用它们后立即销毁它们是否安全?例如:

// 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 );

现在可以使用pthread_mutexattr_destroy()安全地销毁属性,还是需要等到使用pthread_mutex_destroy()销毁互斥锁​​之后?

这同样适用于使用属性的其他pthread对象吗?

1 个答案:

答案 0 :(得分:8)

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

  

在使用互斥锁属性对象初始化一个或之后   更多互斥锁,任何影响属性对象的函数(包括   破坏)不应影响任何先前初始化的互斥锁。

因此,在创建互斥锁之后销毁mutexattr对象是完全安全的。