我可以在使用此 mutexattr 初始化的 mutex 之前销毁 mutexattr 对象吗?
May I destroy a mutexattr object before the mutex that was initialized with this mutexattr is used?
考虑以下代码:
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
pthread_mutex_t mut;
pthread_mutex_init(&mut, &attr);
pthread_mutexattr_destroy(&attr);
pthread_mutex_lock(&mut);
此代码有效吗?
如果允许互斥锁包含对其初始化的属性对象的引用,那么我想我可能不会在使用互斥锁之前调用 pthread_mutexattr_destroy(&attr)
。
就像manual说的那样:
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.
所以你的代码是有效的(但不要忘记检查这些函数的潜在错误)。
考虑以下代码:
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
pthread_mutex_t mut;
pthread_mutex_init(&mut, &attr);
pthread_mutexattr_destroy(&attr);
pthread_mutex_lock(&mut);
此代码有效吗?
如果允许互斥锁包含对其初始化的属性对象的引用,那么我想我可能不会在使用互斥锁之前调用 pthread_mutexattr_destroy(&attr)
。
就像manual说的那样:
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.
所以你的代码是有效的(但不要忘记检查这些函数的潜在错误)。