调用 pthread_spin_destroy() 时释放了哪些 "resources"?
What exact "resources" are released, when calling pthread_spin_destroy()?
我对 phread_spin_destroy() 函数有疑问。在posix标准中定义如下:
The pthread_spin_destroy() function shall destroy the spin lock referenced by lock and release any resources used by the lock.
所以如果有2个函数。一个被许多线程调用的函数持有一个 spinock 并递增例如一个全局变量(在 foo() 调用的示例中)。
和另一个函数(在例如 destroy() 被调用),在第一个函数被调用并调用 pthread_spin_destroy() 之后被一个线程调用。
例如,这些函数可能如下所示:
void* foo(void* i) {
pthread_spin_lock(&LOCK); //lock the spinlock
sleep(2); //sleep a little
globalvariable++; //increment
printf("globalvariable: %d\n", globalvariable); //print for debug purpose
pthread_spin_unlock(&LOCK); //spinlock gets unlocked
return NULL;
}
void* destroy(void* i) {
sleep(5); //sleep
pthread_spin_destroy(&LOCK); //destroy the lock
return NULL; //return
}
请注意,"LOCK" 是类型 pthread_spin_t 的全局声明变量,并且 LOCK 在之前使用 (pthread_spin_init(&LOCK,0)) 初始化foo() 被调用。
调用 destroy() 之前的输出符合预期:函数递增全局变量非常缓慢(因为 sleep(2))。
但是如果我调用 destroy() 函数,则没有任何变化。这部分让我感到困惑。
我的问题是:
我对 pthread_spin_destroy() 的理解有误吗?它只释放资源 "LOCK" (我的意思是这个 pthread_spin_t LOCK 变量)?
我希望自旋锁被破坏,其他线程可以像没有锁一样工作。
提前谢谢你
pthread_spin_destroy()
释放的资源是实现需要分配的任何资源,以实现自旋锁作为 pthread_spin_init()
的一部分(这取决于实现,可能是 "none at all") .
在调用 pthread_spin_destroy()
之后再次调用 pthread_spin_lock()
是未定义的行为,直到再次调用 pthread_spin_init()
。它只是一个释放函数:不要调用它,除非你完全完成锁并且不需要再次锁定它(通常,你会在嵌入另一个的自旋锁上调用 pthread_spin_destroy()
您即将释放的数据结构)。
我对 phread_spin_destroy() 函数有疑问。在posix标准中定义如下:
The pthread_spin_destroy() function shall destroy the spin lock referenced by lock and release any resources used by the lock.
所以如果有2个函数。一个被许多线程调用的函数持有一个 spinock 并递增例如一个全局变量(在 foo() 调用的示例中)。
和另一个函数(在例如 destroy() 被调用),在第一个函数被调用并调用 pthread_spin_destroy() 之后被一个线程调用。
例如,这些函数可能如下所示:
void* foo(void* i) {
pthread_spin_lock(&LOCK); //lock the spinlock
sleep(2); //sleep a little
globalvariable++; //increment
printf("globalvariable: %d\n", globalvariable); //print for debug purpose
pthread_spin_unlock(&LOCK); //spinlock gets unlocked
return NULL;
}
void* destroy(void* i) {
sleep(5); //sleep
pthread_spin_destroy(&LOCK); //destroy the lock
return NULL; //return
}
请注意,"LOCK" 是类型 pthread_spin_t 的全局声明变量,并且 LOCK 在之前使用 (pthread_spin_init(&LOCK,0)) 初始化foo() 被调用。
调用 destroy() 之前的输出符合预期:函数递增全局变量非常缓慢(因为 sleep(2))。
但是如果我调用 destroy() 函数,则没有任何变化。这部分让我感到困惑。
我的问题是:
我对 pthread_spin_destroy() 的理解有误吗?它只释放资源 "LOCK" (我的意思是这个 pthread_spin_t LOCK 变量)?
我希望自旋锁被破坏,其他线程可以像没有锁一样工作。
提前谢谢你
pthread_spin_destroy()
释放的资源是实现需要分配的任何资源,以实现自旋锁作为 pthread_spin_init()
的一部分(这取决于实现,可能是 "none at all") .
在调用 pthread_spin_destroy()
之后再次调用 pthread_spin_lock()
是未定义的行为,直到再次调用 pthread_spin_init()
。它只是一个释放函数:不要调用它,除非你完全完成锁并且不需要再次锁定它(通常,你会在嵌入另一个的自旋锁上调用 pthread_spin_destroy()
您即将释放的数据结构)。