6

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オブジェクトにも当てはまりますか?

4

1 に答える 1

9

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

ミューテックス属性オブジェクトを使用して1つ以上のミューテックスを初期化した後、属性オブジェクトに影響を与える関数(破棄を含む)は、以前に初期化されたミューテックスに影響を与えません。

したがって、ミューテックスの作成が完了した後、mutexattrオブジェクトを破棄することは完全に安全です。

于 2012-06-16T09:39:42.053 に答える