简体   繁体   English

获取结构中 pthread_mutex 初始化的缺少大括号警告

[英]Getting missing-braces warning for pthread_mutex initialization in struct

I have a struct fifo that contains types pthread_mutex_t and pthread_cond_t .我有一个包含类型pthread_mutex_tpthread_cond_tstruct fifo Is there something special about these types that I shouldn't initialize them with {0} ?这些类型有什么特别之处,我不应该用{0}初始化它们吗?

#include <stdio.h>
#include <pthread.h>

struct fifo {
        pthread_mutex_t head_mutex;
        pthread_cond_t cond_add;
};
typedef struct fifo fifo;

fifo* fifo_construct(fifo* self)
{
        *self = (fifo){
            {0}, /* head_mutex */
            {0}, /* cond_add */
        };

        pthread_mutex_init(&self->head_mutex, NULL);
        pthread_cond_init(&self->cond_add, NULL);

        return self;
}

int main(int argc, char** argv)
{
        fifo f;
        fifo_construct(&f);

        pthread_mutex_t mut = {0}; // No complaint here!

        fprintf(stderr, "addr: %p, %p\n", &mut, &f.head_mutex);
}

Using gcc -Wmissing-braces (10.2.0) produces the following warning:使用gcc -Wmissing-braces (10.2.0) 会产生以下警告:

fifo.c: In function ‘fifo_construct’:
fifo.c:12:10: warning: missing braces around initializer [-Wmissing-braces]
   12 |  *self = (fifo){
      |          ^
   13 |      {0}, /* head_mutex */
      |       {}
   14 |      {0}, /* cond_add */
      |       {}

But it doesn't complain about the initialization in main.但它不会抱怨 main 中的初始化。 Am I missing something or is the compiler just being picky?我是不是遗漏了什么或者编译器只是挑剔?

I have a struct fifo that contains types pthread_mutex_t and pthread_cond_t .我有一个包含类型pthread_mutex_tpthread_cond_tstruct fifo Is there something special about these types that I shouldn't initialize them with {0} ?这些类型有什么特别之处,我不应该用{0}初始化它们吗?

Not as far as the C language is concerned.就 C 语言而言不是。 {0} is a valid initializer for an object of any type. {0}是任何类型的 object 的有效初始值设定项。 That does not imply, however, that the resulting mutex or CV value is a valid one as far at the pthreads level.然而,这并不意味着所产生的互斥锁或 CV 值在 pthreads 级别是有效的。

Using gcc -Wmissing-braces (10.2.0) produces the following warning: [...]使用 gcc -Wmissing-braces (10.2.0) 会产生以下警告:[...]

This is a style warning, which you could obtain from similar initializers for any number of similar structure types.这是一个样式警告,您可以从任何数量的相似结构类型的相似初始值设定项中获得。 It does not necessarily imply that there is anything is wrong with your code.这并不一定意味着您的代码有任何问题。 GCC just thinks that it's a risky approach, notwithstanding that the special case of the {0} initializer form is utterly idiomatic. GCC 只是认为这是一种冒险的方法,尽管{0}初始化程序形式的特殊情况完全是惯用的。

Other options include其他选项包括

  • Using the initializer macros that pthreads provides for these types * :使用 pthreads 为这些类型提供的初始化宏*

     *self = (fifo) { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER };

    In this case, you do not need to also call the initialization functions unless you want non-default properties.在这种情况下,您不需要也调用初始化函数,除非您需要非默认属性。

  • Omit any explicit initializers for those members.省略这些成员的任何显式初始值设定项。

  • If, as in the example code, the mutex and CV are the only members, or if you do not care to initialize any others, then drop the whole assignment statement with its compound literal.如果在示例代码中,mutex 和 CV 是唯一的成员,或者如果您不关心初始化任何其他成员,则删除整个赋值语句及其复合文字。 You don't need to pre-initialize the mutex and CV when you are anyway going to initialize them via their initialization functions before using them.当您无论如何要在使用它们之前通过它们的初始化函数初始化它们时,您不需要预先初始化互斥锁和 CV。

  • Turn off the warning with -Wno-missing-braces使用-Wno-missing-braces关闭警告


* Older versions of the POSIX specifications only promise that these can be used for objects having static storage duration, but that caveat has been dropped from recent versions. * POSIX 规范的旧版本只有 promise,这些可以用于具有 static 存储持续时间的对象,但该警告已从最新版本中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM