简体   繁体   English

C ++编译器如何支持C ++ 11原子,但不支持C ++ 11内存模型

[英]How can C++ compilers support C++11 atomic, but not support C++11 memory model

While looking at Clang and g++ C++11 implementation status I noticed something strange: 在查看Clang和g ++ C ++ 11实现状态时,我注意到一些奇怪的事情:
they support C++11 atomics, but they dont support C++11 memory model. 它们支持C ++ 11原子,但它们不支持C ++ 11内存模型。
I was under impression that you must have C++11 memory model to use atomics. 我的印象是你必须有C ++ 11内存模型才能使用原子。 So what exactly is the difference between support for atomics and memory model? 那么对原子和内存模型的支持究竟有什么区别呢?
Does a lack of memory model support means that legal C++11 programs that use std::atomic<T> arent seq consistent? 缺乏内存模型支持意味着使用std::atomic<T>合法C ++ 11程序是不是一致的?

references: 引用:
http://clang.llvm.org/cxx_status.html http://clang.llvm.org/cxx_status.html
http://gcc.gnu.org/gcc-4.7/cxx0x_status.html http://gcc.gnu.org/gcc-4.7/cxx0x_status.html

One of the issues is the definition of "memory location", that allows (and forces the compiler to support) locking different structure members by different locks. 其中一个问题是“内存位置”的定义,它允许(并强制编译器支持)通过不同的锁来锁定不同的结构成员。 There is a discussion about a RL problem caused by this . 讨论由此引起的RL问题

Basically the issue is that having a struct defined like this: 基本上问题是有一个像这样定义的struct

struct x {
    long a;
    unsigned int b1;
    unsigned int b2:1;
};

the compiler is free to implement writing to b2 by overwriting b1 too (and apparently, judging from the report, it does). 编译器也可以通过覆盖b1来实现对b2的写入(显然,从报告来看,它确实如此)。 Therefore, the two fields have to be locked as one. 因此,必须将两个字段锁定为一个字段。 However, as a consequence of the C++11 memory model, this is forbidden (well, not really forbidden, but the compiler must ensure simultaneous updates to b1 and b2 do not interfere; it could do it by locking or CAS-ing each such update, well, life is difficult on some architectures). 但是,作为C ++ 11内存模型的结果,这是禁止的(好吧,不是真的被禁止,但编译器必须确保对b1b2同时更新不会干扰;它可以通过锁定或CAS-ing来实现这样的更新,在一些架构上生活很困难)。 Quoting from the report: 引用报告:

I've raised the issue with our GCC guys and they said to me that: "C does not provide such guarantee, nor can you reliably lock different structure fields with different locks if they share naturally aligned word-size memory regions. The C++11 memory model would guarantee this, but that's not implemented nor do you build the kernel with a C++11 compiler." 我向我们的GCC人员提出了这个问题,他们告诉我:“C不提供这样的保证,如果他们共享自然对齐的字大小的内存区域,也不能用不同的锁定可靠地锁定不同的结构字段.C + +11内存模型可以保证这一点,但是没有实现,也没有使用C ++ 11编译器构建内核。“

Nice info can also be found in the wiki . wiki中也可以找到不错的信息。

I guess the "Lack of memory model" in these cases just means that the optimizers were written before the C++11 memory model got published, and might perform now invalid optimizations. 我想在这些情况下“缺乏内存模型”只意味着优化器是在C ++ 11内存模型发布之前编写的,并且可能现在执行无效的优化。 It's very difficult and time-consuming to validate optimizations against the memory model, so it's no big surprise that the clang/gcc teams haven't finished that yet. 验证针对内存模型的优化是非常困难和耗时的,因此clang / gcc团队还没有完成这一点也就不足为奇了。

Does a lack of memory model support means that legal C++11 programs that use std::atomic arent seq consistent? 缺乏内存模型支持是否意味着使用std :: atomic的合法C ++ 11程序不是seq一致的?

Yes, that's a possibility. 是的,这是可能的。 It's even worse: the compiler might introduce data races into (according to the C++11 standard) race-free programs, eg by introducing speculative writes. 更糟糕的是:编译器可能会引入数据竞争(根据C ++ 11标准)无竞争程序,例如通过引入推测性写入。

For example, several C++ compilers used to perform this optimization: 例如,用于执行此优化的几个C ++编译器:

for (p = q; p = p -> next; ++p) {
    if (p -> data > 0) ++count;
}

Could get optimized into: 可以优化到:

register int r1 = count;
for (p = q; p = p -> next; ++p) {
    if (p -> data > 0) ++r1;
}
count = r1;

If all p->data are non-negative, the original source code did not write to count , but the optimized code does. 如果所有p->data都是非负数,则原始源代码不会写入count ,但优化后的代码会执行。 This can introduce a data race in an otherwise race-free program, so the C++11 specification disallows such optimizations. 这可以在一个无竞争的程序中引入数据竞争,因此C ++ 11规范不允许这样的优化。 Existing compilers now have to verify (and adjust if necessary) all optimizations. 现有的编译器现在必须验证(并在必要时进行调整)所有优化。

See Concurrency memory model compiler consequences for details. 有关详细信息,请参阅并发内存模型编译器结

It's not so much that they don't support the memory model, but that they don't (yet) support the API in the Standard for interacting with the memory model. 它不是支持内存模型,而是它们(还)支持标准中的API以与内存模型交互。 That API includes a number of mutexes. 该API包含许多互斥锁。

However, both Clang and GCC have been as thread aware as possible without a formal standard for some time. 但是,Clang和GCC在一段时间内没有正式标准就已尽可能地了解线程。 You don't have to worry about optimizations moving things to the wrong side of atomic operations. 您不必担心优化将事物移到原子操作的错误一边。

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

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