gpt4 book ai didi

c - 销毁静态互斥体和 rwlock 初始值设定项

转载 作者:太空狗 更新时间:2023-10-29 16:06:39 25 4
gpt4 key购买 nike

假设我们有一个用静态初始化器初始化的全局互斥锁或 rwlock:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;

文档说它等同于带有默认参数的 pthread_*_init

我们是否必须销毁以这种方式初始化的互斥锁或 rwlock?

最佳答案

没有。静态分配和动态分配的互斥量之间的区别基本上类似于位于堆栈或堆中的变量。您不必归还不是动态分配的互斥量。引自 Michael Kerrisk 的“The Linux Programming Interface”:

When an automatically or dynamically allocated mutex is no longer required, it should be destroyed using pthread_mutex_destroy(). (It is not necessary to call pthread_mutex_destroy() on a mutex that was statically initialized using PTHREAD_MUTEX_INITIALIZER.)

关于c - 销毁静态互斥体和 rwlock 初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29295021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com