c - C中的原子参数执行顺序

标签 c atomic

我正在尝试使用 stdatomic.h 函数,特别是 atomic_flag_test_and_set。我没有看到任何错误,但想知道我正在做的事情是否总是安全的。我有如下结构:

typedef struct Mystruct {
    int somedata;
    atomic_flag flag;
} Mystruct;

稍后,当我创建一个 mystruct 并使用它的标志实例时,我会这样做:

if(atomic_flag_test_and_set(&mystructInstance->flag)) {
    // do something
}

&mystructInstance->flag 的评估是否总是在检查原子操作之前完成?我会这么认为,因为它应该是一个处理器指令(或模拟一个处理器指令的东西),但我想确定一下。

最佳答案

Is the evaluation of &mystructInstance->flag always completed before the check for the atomic operation?

这个问题的答案可以在 C 标准的“函数调用”部分找到。

6.5.2.2 Function calls
...
4. An argument may be an expression of any complete object type. In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument.

另请注意,如果一个函数采用多个参数,则传递给它的参数的计算顺序是未指定。标准的同一部分也提到了这一点。

10.There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.

关于c - C中的原子参数执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53825626/

相关文章:

c++ - 发生在同一线程中的关系之前

memory - ARM单拷贝原子性

c - 如何将(长)日期转换为 C 中可读的格式?

c - MakeFile 不编译和构建 .o 文件

c - for循环之谜

C - 锯齿波的傅立叶变换级数

c - 如何根据它们的差异是否最接近给定数字来选择两个数字?

c++ - 原子递减 union 的数据成员?

mysql - InnoDB 中的 'update tokens set tokens = tokens + 1' 是原子的吗?

java - 静态分析期间检查/放置错误的非原子使用