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

时间:2022-12-20 21:03:20

While looking at Clang and g++ C++11 implementation status I noticed something strange:
they support C++11 atomics, but they dont support C++11 memory model.
I was under impression that you must have C++11 memory model to use atomics. 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?

在查看Clang和g++ c++ c++ c++ 11实现状态时,我注意到一些奇怪的东西:它们支持c++ 11原子,但不支持c++ 11内存模型。我的印象是您必须有c++ 11内存模型才能使用atomics。那么,原子模型和内存模型的支持究竟有什么区别呢?缺乏内存模型支持是否意味着使用std::原子 的合法c++ 11程序不一致?

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

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

3 个解决方案

#1


15  

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 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). 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). Quoting from the report:

编译器可以通过覆盖b1来实现对b2的写入(显然,从报告来看,它也可以)。因此,两个字段必须被锁定为一个字段。但是,由于c++ 11内存模型的原因,这是被禁止的(嗯,并不是真的被禁止,但是编译器必须保证对b1和b2的同时更新不受干扰;它可以通过对每一个这样的更新进行锁定或封装来实现,对某些架构来说,生命是困难的)。引用的报告:

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.

在维基中也可以找到不错的信息。

#2


11  

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. 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.

我猜想,在这些情况下,“缺乏内存模型”只意味着优化器是在c++ 11内存模型发布之前编写的,现在可能执行无效的优化。对内存模型验证优化是非常困难和耗时的,所以clang/gcc团队还没有完成这一点也不奇怪。

Does a lack of memory model support means that legal C++11 programs that use std::atomic arent seq consistent?

缺乏内存模型支持是否意味着使用std::atomic arent seq的合法c++ 11程序是一致的?

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, e.g. 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. This can introduce a data race in an otherwise race-free program, so the C++11 specification disallows such optimizations. Existing compilers now have to verify (and adjust if necessary) all optimizations.

如果所有p->数据都是非负的,那么原始的源代码不会写计数,但是优化后的代码会。这可以在没有竞争的程序中引入数据竞争,因此c++ 11规范不允许这种优化。现有编译器现在必须验证(并在必要时进行调整)所有优化。

See Concurrency memory model compiler consequences for details.

有关详细信息,请参见并发内存模型编译器结果。

#3


0  

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. That API includes a number of mutexes.

并不是说它们不支持内存模型,而是说它们(还)不支持标准中的API来与内存模型交互。该API包含许多互斥对象。

However, both Clang and GCC have been as thread aware as possible without a formal standard for some time. You don't have to worry about optimizations moving things to the wrong side of atomic operations.

然而,Clang和GCC在没有正式标准的情况下已经尽可能地了解线程了。您不必担心优化将事情转移到原子操作的错误方面。

#1


15  

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 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). 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). Quoting from the report:

编译器可以通过覆盖b1来实现对b2的写入(显然,从报告来看,它也可以)。因此,两个字段必须被锁定为一个字段。但是,由于c++ 11内存模型的原因,这是被禁止的(嗯,并不是真的被禁止,但是编译器必须保证对b1和b2的同时更新不受干扰;它可以通过对每一个这样的更新进行锁定或封装来实现,对某些架构来说,生命是困难的)。引用的报告:

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.

在维基中也可以找到不错的信息。

#2


11  

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. 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.

我猜想,在这些情况下,“缺乏内存模型”只意味着优化器是在c++ 11内存模型发布之前编写的,现在可能执行无效的优化。对内存模型验证优化是非常困难和耗时的,所以clang/gcc团队还没有完成这一点也不奇怪。

Does a lack of memory model support means that legal C++11 programs that use std::atomic arent seq consistent?

缺乏内存模型支持是否意味着使用std::atomic arent seq的合法c++ 11程序是一致的?

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, e.g. 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. This can introduce a data race in an otherwise race-free program, so the C++11 specification disallows such optimizations. Existing compilers now have to verify (and adjust if necessary) all optimizations.

如果所有p->数据都是非负的,那么原始的源代码不会写计数,但是优化后的代码会。这可以在没有竞争的程序中引入数据竞争,因此c++ 11规范不允许这种优化。现有编译器现在必须验证(并在必要时进行调整)所有优化。

See Concurrency memory model compiler consequences for details.

有关详细信息,请参见并发内存模型编译器结果。

#3


0  

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. That API includes a number of mutexes.

并不是说它们不支持内存模型,而是说它们(还)不支持标准中的API来与内存模型交互。该API包含许多互斥对象。

However, both Clang and GCC have been as thread aware as possible without a formal standard for some time. You don't have to worry about optimizations moving things to the wrong side of atomic operations.

然而,Clang和GCC在没有正式标准的情况下已经尽可能地了解线程了。您不必担心优化将事情转移到原子操作的错误方面。