简体   繁体   English

stdatomic.h function 在普通 int 上 - 产生未定义或实现定义的行为?

[英]stdatomic.h function on plain int - yields undefined or implementation defined behaviour?

With GCC something like this compiles and probably works like expected:使用 GCC 类似这样的东西可以编译并且可能像预期的那样工作:

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

int main(int argc, char **argv)
{
    (void)argc;

    int x   = 23;
    // XXX
    // instead of:
    // _Atomic int x = 23;

    int exp = atoi(argv[1]);
    int y   = atoi(argv[2]);

    printf("x = %d, exp = %d, y = %d\n", x, exp, y);

    bool b = atomic_compare_exchange_strong(&x, &exp, y);

    printf("x = %d, exp = %d, y = %d => %s\n", x, exp, y, b ? "true" : "false");
    return 0;
}

The C reference says about such atomic functions : C 参考说明了这样的原子函数

This is a generic function defined for all atomic object types A. The argument is pointer to a volatile atomic type to accept addresses of both non-volatile and volatile (eg memory-mapped I/O) atomic variables.这是为所有原子 object 类型A 定义的通用 function。参数是指向易失原子类型的指针,以接受非易失和易失(例如内存映射 I/O)原子变量的地址。 C is the non-atomic type corresponding to A. C是A对应的非原子类型。

This implies that the atomic functions like atomic_compare_exchange_strong() are not defined for non-atomic types.这意味着像atomic_compare_exchange_strong()这样的原子函数没有为非原子类型定义。

Since int is a non-atomic type does the above code yields undefined behavior?由于int是非原子类型,上述代码是否会产生未定义的行为?

Or does the C standard specifies this as implementation defined behavior?或者 C 标准是否将其指定为实现定义的行为?

(where an implementation is able to define int as atomic type, as well - thus basically making it equivalent to _Atomic int ) (其中实现也能够将int定义为原子类型 - 因此基本上使其等同于_Atomic int

Not using _Atomic types for atomic functions is definitely undefined behaviour.不为原子函数使用 _Atomic 类型绝对是未定义的行为。 I am little surprised the compiler doesn't complain, but C is pretty forgiving.我有点惊讶编译器没有抱怨,但 C 非常宽容。 Atomic functions provide a very specific guarantee about how they behave when multiple threads are modifying the same memory location at the same time.当多个线程同时修改同一个 memory 位置时,原子函数提供了一个非常具体的保证。 Your simple example does not actually demonstrate this.您的简单示例实际上并未证明这一点。

Read the description of atomic types :阅读原子类型的描述:

Objects of atomic types are the only objects that are free from data races, that is, they may be modified by two threads concurrently or modified by one and read by another.原子类型的对象是唯一没有数据竞争的对象,也就是说,它们可以由两个线程同时修改或由一个线程修改并由另一个线程读取。

By not using atomic types you are no longer guaranteed to be able to access the values in an atomic way, thus making the atomic function useless.通过不使用原子类型,您不再保证能够以原子方式访问值,从而使原子 function 无用。

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

相关问题 <stdatomic.h>在GCC 4.8? - <stdatomic.h> in GCC 4.8? Xcode和C11 stdatomic.h - Xcode and C11 stdatomic.h 可用的编译器是否提供C11'_Atomic'关键字及其相关标题'stdatomic.h'的实现? - Does an available compiler provide an implementation of the C11 '_Atomic' keyword and its related header 'stdatomic.h'? 如何在缺少stdatomic.h的机器上使用原子整数? - How to have atomic integers on machines that lack stdatomic.h? 使用 C11 GCC 使数据读/写原子化<stdatomic.h> ? - Making data reads/writes atomic in C11 GCC using <stdatomic.h>? stdatomic.h 的名称是否与在句点之前映射到八个有效字符的(潜在)限制相矛盾? - Does name of stdatomic.h contradict with (potential) restriction of the mapping to eight significant characters before the period? 做atomic_store /加载 <stdatomic.h> 适用于英特尔的未对齐,跨缓存行数据? - Do atomic_store/load from <stdatomic.h> work for unaligned, cross-cache-line data on Intel? 未定义/未指定/实现定义的行为警告? - Undefined/Unspecified/Implementation-defined behaviour warnings? 有什么方法可以预测未定义的行为或实现的已定义行为? - Is there any way to predict the undefined behaviour or implementation defined behaviour? 签名的 integer 是否溢出未定义的行为或定义的实现? - Is signed integer overflow undefined behaviour or implementation defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM