销毁静态互斥锁和rwlock初始化程序 [英] Destroy static mutex and rwlock initializers

查看:136
本文介绍了销毁静态互斥锁和rwlock初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个使用静态初始值设定项初始化的全局互斥量或rwlock:

Let's suppose we have a global mutex or rwlock initialized with a static initializer:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;

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

Documentation says it's equivalent to pthread_*_init with default parameters.

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

Do we have to destroy a mutex or rwlock initialized this way?

推荐答案

否.静态分配的互斥锁和动态分配的互斥锁之间的差异基本上可以与位于堆栈或堆中的变量相媲美.您不必退还没有动态分配的互斥量. 引自Michael Kerrisk的"Linux编程接口":

No. The difference between a statically allocated and a dynamically allocated mutex is basically comparable to a variable located on the stack or in the heap. You don't have to give a mutex back that you didn't allocate dynamically. Quoting from Michael Kerrisk's "The Linux Programming Interface":

当不再需要自动或动态分配的互斥锁时,应使用pthread_mutex_destroy()将其销毁. (不必在使用静态初始化的互斥锁上调用pthread_mutex_destroy() PTHREAD_MUTEX_INITIALIZER.)

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.)

这篇关于销毁静态互斥锁和rwlock初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆