【发布时间】:2020-08-09 16:53:57
【问题描述】:
For glibc <= 2.23,看起来 malloc 的 mutex_lock 宏的通用定义使用 int 作为互斥体。 1 表示正在使用,0 表示免费。
它定义了这组通用宏:
typedef int mutex_t
# define mutex_init(m) (*(m) = 0)
# define mutex_lock(m) ({ *(m) = 1; 0; })
# define mutex_trylock(m) (*(m) ? 1 : ((*(m) = 1), 0))
# define mutex_unlock(m) (*(m) = 0)
对于mutex_lock(m),0; 的作用是什么?
【问题讨论】:
-
可能记录为成功返回
0? -
0;是一个没有副作用的声明。它没有设置返回值。 -
如果您阅读the GCC statement expression documentation,您会看到“复合语句中的最后一件事应该是一个后跟分号的表达式;这个子表达式的值用作整个构造的值。”简而言之,它将充当返回值。
-
好样的!学到了一些新东西。因此,如果
0;不存在,则返回值为 1。谢谢!
标签: c malloc c-preprocessor glibc