c++ - C和C++中的编译器优化和临时分配

标签 c++ c concurrency embedded

请查看以下在C和C++中有效的代码:

extern int output;
extern int input;
extern int error_flag;

void func(void)
{
  if (0 != error_flag)
  {
    output = -1;
  }
  else
  {
    output = input;
  }
}
  • 是否允许编译器以与下面类似的方式编译上面的代码?
    extern int output;
    extern int input;
    extern int error_flag;
    
    void func(void)
    {
      output = -1;
      if (0 == error_flag)
      {
        output = input;
      }
    }
    

    换句话说,是否允许编译器生成(从第一个片段开始)始终向output临时分配-1的代码,然后根据input状态将output值分配给error_flag
  • 如果将output声明为volatile,将允许编译器执行此操作吗?
  • 如果将output声明为atomic_int(stdatomic.h),将允许编译器执行此操作吗?

  • David Schwartz发表评论后更新:

    如果编译器可以自由地向变量添加其他写操作,则似乎无法从C代码中判断是否存在数据竞争。如何确定呢?

    最佳答案

  • 是的,可以进行推测性分配。非 volatile 变量的修改不是程序可观察到的行为的一部分,因此允许伪写。 (有关“可观察的行为”的定义,请参见下文,它实际上并不包括您可能会观察到的所有行为。)
  • 否。如果outputvolatile,则不允许进行推测性或虚假的突变,因为该突变是可观察到的行为的一部分。 (向硬件寄存器写信息或从中读取信息可能不仅会存储值,还会带来其他后果。这是volatile的主要用例之一。)
  • (编辑)否,atomic output无法进行推测性分配。 atomic变量的加载和存储是同步操作,因此应该不可能加载未明确存储到该变量中的此类变量的值。

  • 可观察的行为

    尽管程序可以执行很多明显的操作(例如,由于段错误而突然终止),但是C和C++标准仅保证有限的结果集。可观察到的行为在C11草案的第5.1.2.3p6节和当前C++ 14草案的第1.9p8节[intro.execution]中定义,措辞非常相似:

    The least requirements on a conforming implementation are:

    — Access to volatile objects are evaluated strictly according to the rules of the abstract machine.

    — At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.

    — The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.

    These collectively are referred to as the observable behavior of the program.



    以上摘自C++标准; C标准的不同之处在于,第二点不允许出现多种可能的结果,第三点明确引用了标准库要求的相关部分。除了细节之外,定义是相互协调的。就此问题而言,相关的一点是只能观察到 volatile 变量(直到将非 volatile 变量的值发送到输出设备或文件为止)。

    数据竞赛

    还应在C和C++标准的整体上下文中阅读此段落,如果程序产生不确定的行为,则它们会将实现从所有要求中解放出来。这就是为什么在上面的可观察到的行为的定义中不考虑段错误的原因:段错误是可能的未定义行为,但在一致性程序中不是可能的行为。因此,在只有一致的程序和一致的实现的世界中,没有段错误。

    这很重要,因为具有数据争用的程序不符合要求。数据争用具有不确定的行为,即使它看起来是无害的也是如此。并且由于程序员有责任避免未定义的行为,因此实现可以在不考虑数据竞争的情况下进行优化。

    在C和C++标准中对内存模型的说明是密集和技术性的,可能不适合作为概念的介绍。 (浏览Hans Boehm's site上的 Material 可能会变得困难一些。)从标准中提取报价是有风险的,因为细节很重要。但是,与当前的C++ 14标准§1.10[intro.multithread]相比,这是一个小小的飞跃:

    1. Two expression evaluations conflict if one of them modifies a memory location and the other one reads or modifies the same memory location.

    1. Two actions are potentially concurrent if

      — they are performed by different threads, or

      — they are unsequenced, and at least one is performed by a signal handler.

      The execution of a program contains a data race if it contains two potentially concurrent conflicting actions, at least one of which is not atomic, and neither happens before the other, except for the special case for signal handlers described below. Any such data race results in undefined behavior.



    这里的要点是,必须同步读取和写入同一变量;否则,这是一场数据争夺,其结果是不确定的行为。一些程序员可能会反对这种禁止的严格性,认为某些数据竞争是“良性的”。这是Hans Boehm's 2011 HotPar paper "How to miscompile programs with "benign" data races" (pdf)(作者的摘要:“没有良性的数据竞争”)的主题,他向我解释了所有这些。

    此处的同步包括对atomic类型的使用,因此并发读取和修改atomic变量不是数据竞争。 (读取的结果是不可预测的,但它必须是修改前的值或修改后的值。)这可防止编译器在没有进行任何显式同步的情况下对原子变量执行“逐项”修改。

    经过一番思考和更多研究,我的结论是,编译器也无法对原子变量执行推测性写入。因此,我修改了对问题3的回答,该问题本来是我回答“否”的。

    其他有用的引用资料:
  • Bartosz Milewski:Dealing with Benign Data Races the C++ Way

    Milewski处理对原子变量的推测性写操作的精确问题,并得出以下结论:

    Can’t the compiler still do the same dirty trick, and momentarily store 42 in the owner variable? No, it can’t! Since the variable is declared atomic the compiler can no longer assume that the write can’t be observed by other threads.

  • Thread Safety and Synchronization上的草药萨特

    与往常一样,提供易于访问且写得很好的解释。

    关于c++ - C和C++中的编译器优化和临时分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34863148/

    相关文章:

    c++ - 为什么调用拷贝构造函数

    c++ - 优化 boost::spirit::qi 解析器

    c++ - 我如何计算这个函数的运行时间?

    c++ - 如何使用 C++ 保存网页? Windows或Linux系统

    c - 字符串使用的字符单元格数

    c - 动态内存分配问题(C语言)

    c++ - 如何简化这段代码?

    c - 错误是在语法中还是在代码中?

    java - 创建 Java 作业池

    c# - 消费者线程如何将结果返回给正确的生产者线程?