简体   繁体   English

我可以将 C11 `_Atomic` 关键字应用于枚举类型吗?

[英]Can I apply the C11 `_Atomic` keyword to enum types?

If I have a type如果我有一个类型

enum foo {
    FOO,
    BAR,
    BAZ,
};

can I then declare an atomic version of that type like然后我可以声明该类型的原子版本吗

_Atomic(enum foo);

or do I have to use eg atomic_int and cast the result of atomic_load() ?还是我必须使用例如atomic_intatomic_load()的结果?

The following program compiles without warnings:以下程序编译时没有警告:

    #include <stdatomic.h>
    #include <stdio.h>

    enum foo {FOO, BAR, BAZ};

    int main(void) {
        _Atomic(enum foo) foo_a;
        atomic_store(&foo_a, BAR);
        enum foo val = atomic_load(&foo_a);
        printf("%u\n", val);
        return 0;
    }

but so does:但也是如此:

    #include <stdatomic.h>
    #include <stdio.h>

    enum foo {FOO, BAR, BAZ};

    int main(void) {
        enum foo foo; // <---- non atomic
        atomic_store(&foo, BAR);
        enum foo val = atomic_load(&foo);
        printf("%u\n", val);
        return 0;
    }

Yes, all data types can be atomic, and there is no need to use the atomic generic functions for these.是的,所有数据类型都可以是原子的,不需要为这些使用原子泛型函数。 All operations with such an object is then atomic.对这样一个对象的所有操作都是原子的。

For your second example it is weird that you compiler does not issue a warning.对于您的第二个示例,您的编译器没有发出警告很奇怪。 Using a non-atomic for an atomic operation is a constraint violation, so the compiler should give you a diagnostic.对原子操作使用非原子是违反约束的,因此编译器应该给你一个诊断。

Yes this is legal.是的,这是合法的。 BTW, you don't actually need the parens, _Atomic enum foo foo;顺便说一句,你实际上并不需要括号, _Atomic enum foo foo; is equivalent.是等价的。

_Atomic works like other type qualifiers, like const and volatile . _Atomic工作方式与其他类型限定符类似,例如constvolatile


enum foo foo;
atomic_store(&foo, BAR);

is an error with clang .是 clang 的错误 error: address argument to atomic operation must be a pointer to _Atomic type ('enum foo *' invalid) . error: address argument to atomic operation must be a pointer to _Atomic type ('enum foo *' invalid) (From the Godbolt compiler explorer ). (来自Godbolt 编译器资源管理器)。


It's just a quirk of GCC's implementation that it compiles without even a warning, even at -Wall .这只是 GCC 实现的一个怪癖,它甚至在-Wall处编译时甚至没有警告。 That should probably be changed...应该可以改了吧……

GCC's atomic builtins like void __atomic_store_n (type *ptr, type val, int memorder) take a pointer to a plain type, without requiring _Atomic . GCC 的原子内置void __atomic_store_n (type *ptr, type val, int memorder)void __atomic_store_n (type *ptr, type val, int memorder)接受一个指向普通类型的指针,而不需要_Atomic

C++11 <atomic> uses these builtins. C++11 <atomic>使用这些内置函数。 Similarly, GCC's C11 stdatomic.h uses同样,GCC 的 C11 stdatomic.h使用

#define atomic_store_explicit(PTR, VAL, MO)                             \
  __extension__                                                         \
  ({                                                                    \
    __auto_type __atomic_store_ptr = (PTR);                             \
    __typeof__ (*__atomic_store_ptr) __atomic_store_tmp = (VAL);        \
    __atomic_store (__atomic_store_ptr, &__atomic_store_tmp, (MO));     \
  })

#define atomic_store(PTR, VAL)                          \
  atomic_store_explicit (PTR, VAL, __ATOMIC_SEQ_CST)

(The __extension__ is for the GNU C statement-expression, where x = {foo; bar;} takes the value of bar .) __extension__用于 GNU C 语句表达式,其中x = {foo; bar;}bar的值。)

So none of this actually requires that the pointer type has _Atomic .因此,这些实际上都不需要指针类型具有_Atomic

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

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